

/* image change preload */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var valid;
function ValidateContactForm(form) {
valid = true;
var msg1 = ""; 
    if($('#txtName').attr('value') == ""){
        msg1 += "* Required full name <br/>";
        $('#txtName').focus();       
        valid=false;
    }    
    if(getSelectedIndex(document.getElementById('ddlCountry')) == 0){
        msg1 += "* Required country <br/>";
        $('#ddlCountry').focus();       
        valid=false;
    }    
    if(($('#txtEmail').attr('value') == "" || !isValidEmail($('#txtEmail').attr('value')))){
        msg1 += "* Invalid email <br/>";
        $('#txtEmail').focus();
        valid=false;
    }    
    if($('#txtPhone').attr('value') == ""){
        msg1 += "* Required phone number <br/>";
        $('#txtPhone').focus();       
        valid=false;
    } 
    if(getSelectedIndex(document.getElementById('ddlSubject')) == 0){
        msg1 += "* Required inquiry subject <br/>";
        $('#ddlSubject').focus();       
        valid=false;
    } 
//    if($('#txtInstallID').attr('value') == ""){
//        msg1 += "* Required install id <br/>";
//        $('#txtInstallID').focus();       
//        valid=false;
    //} 
    if($('#txtMessage').attr('value') == ""){
        msg1 += "* Required message <br/>";
        $('#txtMessage').focus();       
        valid=false;
    } 
    if(valid){
    $('#Message').html("Sending...");
        $.get("send.aspx",{Name : $('#txtName').attr('value')
                        ,Country : $('#ddlCountry').get(0).options[$('#ddlCountry').get(0).selectedIndex].innerHTML
                        ,Email : $('#txtEmail').attr('value') 
                        ,Phone : $('#txtPhone').attr('value') 
                        ,Subject : $('#ddlSubject').get(0).options[$('#ddlSubject').get(0).selectedIndex].innerHTML
                        ,Company : $('#txtCompany').attr('value') 
                        ,InstallID : $('#txtInstallID').attr('value')
                        ,Message : $('#txtMessage').attr('value')
                        ,SubjectIndex : $('#ddlSubject').get(0).selectedIndex
                        },function(data)
                        {$('#Message').html("Message sent"); 
                         $('#inputContainer input').attr('value','');
                         $('#inputContainer textarea').attr('value','');
                         $('#inputContainer #ddlCountry').get(0).selectedIndex = 0;
                         $('#inputContainer #ddlSubject').get(0).selectedIndex = 0;
                        });
    }
    else{
        $("#msg1").html(msg1);
    }
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function getSelectedIndex(ddl)
{
    for(var i = 0; i < ddl.options.length; i++)
        if(ddl.options[i].selected == true)
           return i;
 }
 
 function getSelectedValue(ddl)
{
    for(var i = 0; i < ddl.options.length; i++)
        if(ddl.options[i].selected == true)
           return ddl.options[i].value;
 }
 var data = "";
 function generateBarcode()
 {
    var url = $('#rdoQRCode').attr("checked") == true ? "http://encode.i-nigma.com/QRCode/img.php" : "http://encode.i-nigma.com/DMtrx/img.php";
    var title = encodeURIComponent($('#txtTitle').attr("value"));
    var size = encodeURIComponent(getSelectedValue($('#ddlSize').get(0)));
    var imgUrl;
    
    switch(BType)
        {
            case "trWebAddress": data = $('#txtWebAddress').attr("value"); break;
            case "trMessage": data = $('#txtMessage').attr("value"); break;
            case "trContact": data = "BEGIN:VCARD\nN:" + $('#txtContactName').attr("value") + "\nTEL:" + $('#txtContactPhone').attr("value") + "\nEMAIL:" + $('#txtContactEmail').attr("value") + "\nEND:VCARD"; break;
            case "trSMS": data = "SMSTO:" + $('#txtSMSPhone').attr("value") + ":" + $('#txtSMSText').attr("value"); break;
        }
        
    if(data == "" || data == null || typeof(data) == 'undefined')
    {
        $('#imgBarcode').attr('src','http://3gvision.com/innerData/images/barcodeFrame.jpg');
    }
    else
    {
        imgUrl = url + "?d=" + encodeURIComponent(data) + "&c=" + title + "&s=" + size;
        $('#imgBarcode').attr('src',imgUrl);
        $('#txtEmbed').attr('value',"<img src=" + imgUrl + " />");
    }
 }
 var BType;
 function setDataTitle(id)
 {
    $('.dataGroup').hide();
    $('#' + id).show();
    BType = id;
 }
 
 function initHelpBoxes()
 {
    $('.HelpBtn').click(function(){$('.HelpBtn').next().hide(); $(this).next().toggle();})
    $('.HelpBox .top span').click(function(){$(this).parent().parent().parent().parent().parent().hide();})
 }
 
 
function InitDevices()
{
        if(typeof($('#divDevices').get(0)) == 'undefined')
            return;
            
        $('#ddlCategories').addOption('Show all supported devices...', 'Show all supported devices...');            
        $('#divDevices .tblDevicesContainer th').each(function(){
            $('#ddlCategories').addOption($(this).html().replace('&amp;', '&'), $(this).html().replace('&amp;', '&'));
        });
        
        $('#divDevices img').each(function(){
            var name = GetCleaenDeviceName($(this).attr('src'));
            var brand = $(this).closest('.tblDevicesContainer').find('th').html();
            $(this).closest('table').find('td:eq(1)').html(name);
            $(this).attr('alt',brand.replace('&amp;', '&') + ' - ' + name);
        });
        
        $('#ddlCategories').get(0).selectedIndex = 0;
        $('#ddlCategories').change(function(){
        var selectedIndex = $('#ddlCategories').get(0).selectedIndex;
            $('#divDevices table').hide();
            if(selectedIndex == 0)
            {
                $('#divDevices table').show(); 
            }
            else
            {
                $('#divDevices .tblDevicesContainer:eq(' + (selectedIndex - 1) + ')').fadeIn("normal");
                $('#divDevices .tblDevicesContainer:eq(' + (selectedIndex - 1) + ') table').fadeIn("normal");
            }
        });
}

function GetCleaenDeviceName(src)
{
    var name = src.split('/')[src.split('/').length - 1].split('.')[0];
    return name.replace(name.substring(0,name.indexOf('_') + 1), '').replace('_', ' ').replace('_', ' ').replace('_', ' ').replace('_', ' ').replace('_', ' ').replace('%20', ' ').replace('%20', ' ');
}

$(document).ready(function(){
    if($('#Credits').length > 0 && location.href.indexOf('html') == -1 || location.href.indexOf('/hp.html') > -1)
        $('#Credits').show();
    else
        $('#Credits').hide();
});

/*$(document).ready(function(){
    if(location.href.indexOf('/CreateBarcodes.html') > -1)
        setDataTitle('trWebAddress');
});*/
