﻿// JScript File

        // ------------------------------------------------------------------------------------------------------
        // this will stop users from leaving the site, will ask for a conf.
        // just what we need for ajax 
        //              Janaka
         
        //        var needToConfirm = true;

        //        window.onbeforeunload = confirmExit;
        //        function confirmExit()
        //        {
        //            if (needToConfirm)
        //                return "You are about to leave Natural Visions website.";
        //        }

        // ------------------------------------------------------------------------------------------------------
        
        // used to hold the id of the timer function
        var myInterval;

        
        function pageTopLinkClick(state){
                //alert(state);
             if (state == 1)   
             {
                Anthem_InvokePageMethod('LinkHome_Click',[],
                 function() {             
                    
                }
                );
             }
             else if (state == 2)  
             {
                Anthem_InvokePageMethod('lnkBtnAboutUs_Click',[],
                 function() {             
                    
                }
                );
             }              
        }


// ------------------------------------------------------------------------------------------------------

//---------------------------------- Used to handle colour swatch onclick -----------------------------

function gotoPage(hostingurl,colourid,guid)
{
       var strUrl = hostingurl + "adsearch.aspx?tconid=2&bconid=9&colorid=" + colourid + "&guid=" + guid + "&adsv=" + document.getElementById("ctl00_TopPanel1_ApplicationAdvancedSearch1_txtAdvanceSerarch").value;
    location.replace(strUrl);
   
}


//------------------------------------------------------------------------------------------------------
 function showSearchResults(){
   
             Anthem_InvokePageMethod('showResults',[],
             function() {             
                //document.getElementById('c').value = result.value;
            }
            );
        }


        
        // ------------------------------------------------------------------------------------------------------
        // advance search functions
        // ------------------------------------------------------------------------------------------------------
        var match_elements  = /[a-z0-9]/i;
        var TXT_BOX_ID      = 'ApplicationAdvancedSearch1_txtAdvanceSerarch';
        var DIV_BOX_ID      = 'SuggestiveSerarchResults';
        

        var mDivMOver = false;
        document.onclick = function() 
        {
            if (!mDivMOver)
            {
                AdvSearch_HideSuggestList('SuggestiveSerarchResults');
                AdvSearch_HideSuggestList('SuggestiveBasicSerarchResults');
            }
        
        }
        
        function HideAllSuggestLists()
        {
            AdvSearch_HideSuggestList('SuggestiveSerarchResults');
            AdvSearch_HideSuggestList('SuggestiveBasicSerarchResults');
        }
                	
        function AdvSearch_ShowSuggestList(DivID)
        {
           
            
            if(document.getElementById(DivID) != null) 
            {

                document.getElementById(DivID).style.visibility = 'visible';
                document.getElementById(DivID).style.display='block';
                document.getElementById('KeywordDropDownPanel').style.visibility = 'hidden';                        
            }                
        }

        function AdvSearch_HideSuggestList(DivID)
        {
            //alert(DivID);
            if(document.getElementById(DivID) != null)
            {
                
                document.getElementById(DivID).style.display='none';
                document.getElementById('KeywordDropDownPanel').style.visibility = 'visible';                       
            
            }
        }

        function AdvSearch_ChkClicked(chkID ,DivID)
        {
             
             
             var chkObj  = document.getElementById(chkID);
            if (!chkObj.checked)
            {
                AdvSearch_HideSuggestList(DivID);
            }           
        }
                
        function AdvSearch_FindSuggests(userControlID,chkID,txtID,divID)
        {

                         
            var chkObj  = document.getElementById(chkID);
            
            if(event.keyCode == 13)
            {
                //alert("enter pressed");
                Anthem_InvokeControlMethod(
                        userControlID,
                        'Search',
                        [], null );
                return;
            }
            
            if (chkObj.checked)
            {
                var testVal;
                testVal = document.getElementById(txtID).value;
                                
                testVal = testVal.replace('\s+', ' ');
                
               

                if (testVal.length == 0 || !match_elements.test(testVal)) 
                {
                    AdvSearch_HideSuggestList(divID);
                    return '';
                }
                
                else
                {
                    
                    Anthem_InvokeControlMethod(
                        userControlID,
                        'BuildSuggestList',
                        [testVal],
                        function(result) {
    					    
                            if (result.value.length > 0)
                            {
                                
                                AdvSearch_ShowSuggestList(divID);							
                                document.getElementById(divID).innerHTML = result.value;
                            }
                            else
                            {
                                
                                AdvSearch_HideSuggestList(divID);
                                document.getElementById(txtID).focus();							    
    						}
                        }
                    );
    			    
                }
            }
            else
            {
                AdvSearch_HideSuggestList(divID);
            }
            
        }
        
        function AdvSearch_KeywordSelected(keyword,txtID,divID)
        {
            document.getElementById(txtID).value = keyword;
            AdvSearch_HideSuggestList(divID);
            document.getElementById(txtID).focus();
        }  
        
        function colourswatch_click(userControlID,colorid,newLocation,historyData,guid)
        {
            AddBackEntry(newLocation, historyData);
            Anthem_InvokeControlMethod(
                        userControlID,
                        'ColourSwatchClicked',
                        [colorid,guid],
                        function(result) {                        
                            AdvSearch_HideSuggestList('SuggestiveSerarchResults');
                            RemoveTimers();
                            ClearCheckedItems();
                            Anthem_PostCallBack();                        
                        }
                    );
        }
        
//        function ResultControl_SelectedPhotoChanged(userControlID, isSelected, photoID)
//        {
//            Anthem_InvokeControlMethod(
//                        userControlID,
//                        'SetPhotoSelectedChanged',
//                        [isSelected, parseInt(photoID)],
//                        null //function(result) {}
//                    );            
//        }
//        
//        function ResultControl_ShowImgDetails(userControlID, photoID)
//        {
//            Anthem_InvokeControlMethod(
//                        userControlID,
//                        'ShowImageDetails',
//                        [photoID],
//                        null //function(result) {}
//                    );
//            return false;
//        }
// -------------------------------------------------------------------------------------
// User Details/Register Functions    
// -------------------------------------------------------------------------------------       
        function ValidateCtrl(ctrlId , typeName)
        {
            var txtBx = document.getElementById(ctrlId);
            var t1 = txtBx.value;
            
           if(t1=="")
           {
              return typeName;  
           }
            return "";
        }
        
        function PwdConfirm(pwd, pwdConf){
            var pass = document.getElementById(pwd).value;
            var conf = document.getElementById(pwdConf).value; 
            
            if(pass!=conf){
                return "false";
            }else{
                return "true";
            }            
               
        }
    
        function CheckAll(userControlID, email, pwd, fname, lname, addr1, city, zip, tel, pwdConf)
        {
            var strOp = "";
            var temp = "";
            temp = ValidateCtrl(email, "- Email cannot be empty \n");
            strOp =  strOp + temp;  
            temp = ValidateCtrl(pwd, "- Password cannot be empty \n");
            strOp =  strOp + temp;   
            temp = ValidateCtrl(fname, "- First Name cannot be empty \n");
            strOp =  strOp + temp;    
            temp = ValidateCtrl(lname, "- Last Name cannot be empty \n");
            strOp =  strOp + temp;     
            temp = ValidateCtrl(addr1, "- Address1 cannot be empty \n");
            strOp =  strOp + temp;   
            temp = ValidateCtrl(city, "- City cannot be empty \n");
            strOp =  strOp + temp;   
            temp = ValidateCtrl(zip, "- Zip code cannot be empty \n");
            strOp =  strOp + temp;   
            temp = ValidateCtrl(tel, "- Telephone number cannot be empty \n");
            strOp =  strOp + temp;    
            var conf = PwdConfirm(pwd, pwdConf);
             if(strOp!="")
             {
                alert(strOp);               
                return false;
                
             }else if(conf=="false"){
                 alert("Please Confirm your password");               
                return false;
                
             }else{
                if(checkEmail(email))
                {
                     Anthem_InvokeControlMethod(
                            userControlID,
                            'imgBtnUpdate_Click',
                            [],
                            function(result)
                            {
                                checkCallback(result);
                                Anthem_PostCallBack();
                                
                            }
                        );
                }else
                {
                    return false;
                }
             }       
        }
        
        function checkCallback(result){
            if (!(result.value == "" ||result.value == null)) 
            {
                alert(result.value);
            }
        }
        
        function ShowRegisterErrorMessage(msg) {
                   alert(msg);
                }
        
        function checkEmail(emField){ //reference to email field passed as argument

var fieldValue = document.getElementById(emField).value // store field's entire value in variable

// Begin Valid Email Address Tests

//if field is not empty
if(fieldValue != ""){ 
var atSymbol = 0

//loop through field value string
for(var a = 0; a < fieldValue.length; a++){ 

//look for @ symbol and for each @ found, increment atSymbol variable by 1
if(fieldValue.charAt(a) == "@"){ 
atSymbol++
}

}

// if more than 1 @ symbol exists
if(atSymbol > 1){ 
// then cancel and don't submit form
alert("Please Enter A Valid Email Address"); 
return false;
}

// if 1 @ symbol was found, and it is not the 1st character in string
if(atSymbol == 1 && fieldValue.charAt(0) != "@"){ 
//look for period at 2nd character after @ symbol 
var period = fieldValue.indexOf(".",fieldValue.indexOf("@")+2) 

// "." immediately following 1st "." ? 
var twoPeriods = (fieldValue.charAt((period+1)) == ".") ? true : false 

//if period was not found OR 2 periods together OR field contains less than 5 characters OR period is in last position
if(period == -1 || twoPeriods || fieldValue.length < period + 2 || fieldValue.charAt(fieldValue.length-1)=="."){
// then cancel and don't submit form
alert("Please Enter A Valid Email Address"); 
return false;
}

}
// no @ symbol exists or it is in position 0 (the first character of the field)
else{ 
// then cancel and don't submit form
alert("Please Enter A Valid Email Address");
return false; 
}
}
// if field is empty
else{ 
// then cancel and don't submit form
alert("Please Enter A Valid Email Address");
return false; 
}

//all tests passed, submit form
//alert("VALID EMAIL ADDRESS!")
return true
}
        
// ------------------------------------------------------------------------------------------------------  
function callSimpleSearch_withTiming(userControlID,chkID,txtID,divID){
var chkObj          = document.getElementById(chkID);

    //alert('callSimpleSearch_withTiming');          
    if (chkObj.checked)
    {
        var testVal;
        testVal         = document.getElementById(txtID).value;
                        
        testVal         = testVal.replace('\s+', ' ');                                       
        
        //alert(testVal);
        
        if (testVal.length == 0 || !match_elements.test(testVal)) 
        {
            AdvSearch_HideSuggestList(divID);
            return '';
        }
        
        else
        {
                        
            
            Anthem_InvokeControlMethod(
                userControlID,
                'BuildSuggestList',
                [testVal],
                function(result) {
				    
				    
				    
                    if (result.value.length > 0)
                    {
                        
                        AdvSearch_ShowSuggestList(divID);							
                        
                        
                        
                        document.getElementById(divID).innerHTML = result.value;
                    }
                    else
                    {
                        
                        AdvSearch_HideSuggestList(divID);
                        document.getElementById(txtID).focus();						

	    
				    }
                }
            );
		    
        }
    }
    else
    {
        AdvSearch_HideSuggestList(divID);
    }                

    return true;
}

function RemoveTimers()
{    
    if (myInterval != null)
    {
        
        window.clearTimeout(myInterval);
        delete myInterval;

    }
}

// with timer
function simple_search_keyup(e,userControlID,chkID,txtID,divID)
{
    var keynum;
    var keychar;  
    var testVal;  
    //alert("Hi");
  if(document.getElementById('chkBasicSuggestive').checked==true)
   {
    if(window.event) // IE
    {
        keynum              = e.keyCode;       
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum              = e.which;
    }
    //alert(keynum);
    // modified by aruna (if codition added)
    if(keynum != 13)
    {
        myInterval = window.setTimeout("callSimpleSearch_withTiming('"+userControlID+"','"+chkID+"','"+txtID+"','"+divID+"')",2000);
         //var myInterval = window.setTimeout(function(chkID,keynum,userControlID,txtID,divID){
              
        //},2000,chkID,keynum,userControlID,txtID,divID);
        window.setTimeout(function () {  clearInterval(myInterval);},3000);
    }
    }
    else
    {
        return true;
    }
    
}
//---------------------------------------------------------------------  
// with out timer        
function simple_search_keyup_withouttimer(e,userControlID,chkID,txtID,divID)
{
    var keynum;
    var keychar;  
    var testVal;  
    
    if(document.getElementById('chkBasicSuggestive').checked==true)
   {
    if(window.event) // IE
    {
        keynum              = e.keyCode;       
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum              = e.which;
    }
    
     //var myInterval = window.setTimeout(function(chkID,keynum,userControlID,txtID,divID){
          
    var chkObj          = document.getElementById(chkID);

                
    if (chkObj.checked)
    {
        var testVal;
        testVal         = document.getElementById(txtID).value;
                        
        testVal         = testVal.replace('\s+', ' ');                                       
        
        //alert(testVal);
        
        if (testVal.length == 0 || !match_elements.test(testVal)) 
        {
            AdvSearch_HideSuggestList(divID);
            return '';
        }
        
        else
        {
                        
            
            Anthem_InvokeControlMethod(
                userControlID,
                'BuildSuggestList',
                [testVal],
                function(result) {
				    
				    
				    
                    if (result.value.length > 0)
                    {
                        
                        AdvSearch_ShowSuggestList(divID);							
                        
                        
                        
                        document.getElementById(divID).innerHTML = result.value;
                    }
                    else
                    {
                        
                        AdvSearch_HideSuggestList(divID);
                        document.getElementById(txtID).focus();						

	    
				    }
                }
            );
		    
        }
    }
    else
    {
        AdvSearch_HideSuggestList(divID);
    }                
}

    return true;//},2000,chkID,keynum,userControlID,txtID,divID);
    //window.setTimeout(function () {  clearInterval(myInterval);},3000);
}

function simple_search_onkeypress(e,userControlID,chkID,txtID,divID,guid)
{
    var keynum;
    var keychar;  
    var testVal;  
    
    //alert("keypress");
    
    if(window.event) // IE
    {
        keynum              = e.keyCode;       
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum              = e.which;
    }
    
          
    if (keynum == 13)
    {
        //alert(keynum);
        //alert(userControlID);
        //alert("enter pressed");
        
        
        testVal             = document.getElementById(txtID).value;
                                    
        testVal             = testVal.replace('\s+', ' '); 
        
        //alert(testVal);
        
//        Anthem_InvokeControlMethod(
//                userControlID,
//                'SimpleSearch',
//                [testVal], null );
        
        RemoveTimers();
        AddBackEntry('nv_home1' + guid ,userControlID+'!simple_search_click!'+guid,userControlID,guid);
        Anthem_InvokeControlMethod(
                userControlID,
                'SimpleSearchForEnterButton',
                [testVal,guid], function(){
                    AdvSearch_HideSuggestList('SuggestiveSerarchResults');
                    RemoveTimers();
                    ResetSearchOption('ApplicationSearch1_drpKeywordOption');
                    Anthem_PostCallBack();
                    
                    } 
                 );

        
        e                   = null;
        return false;
    }
    
    return true;
    
    //document.getElementById(txtID).focus();
    //return false;
    
    //keychar                 = String.fromCharCode(keynum);
    //return keychar;
    /*
    else
    {
        keychar             = String.fromCharCode(keynum);
                        
        var chkObj          = document.getElementById(chkID);

        
                
        if (chkObj.checked)
        {
            var testVal;
            testVal         = document.getElementById(txtID).value + keychar;
                            
            testVal         = testVal.replace('\s+', ' ');                                       
            
            //alert(testVal);
            
            if (testVal.length == 0 || !match_elements.test(testVal)) 
            {
                AdvSearch_HideSuggestList(divID);
                return '';
            }
            
            else
            {
                
                Anthem_InvokeControlMethod(
                    userControlID,
                    'BuildSuggestList',
                    [testVal],
                    function(result) {
					    
                        if (result.value.length > 0)
                        {
                            
                            AdvSearch_ShowSuggestList(divID);							
                            document.getElementById(divID).innerHTML = result.value;
                        }
                        else
                        {
                            
                            AdvSearch_HideSuggestList(divID);
                            document.getElementById(txtID).focus();						
	    
					    }
                    }
                );
			    
            }
        }
        else
        {
            AdvSearch_HideSuggestList(divID);
        }                
        
        e                   = null;
        return true;
    }
    */
    
}


function advanced_search_onkeypress(e,userControlID,chkID,txtID,divID,guid)
{
    var keynum;
    var keychar;  
    var testVal;  
    
    //alert("keypress");
    
    if(window.event) // IE
    {
        keynum              = e.keyCode;       
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum              = e.which;
    }
    
            
    if (keynum == 13)
    {
        //alert(keynum);
        //alert(userControlID);
        //alert("enter pressed");
        
        
        testVal             = document.getElementById(txtID).value;
                                    
        testVal             = testVal.replace('\s+', ' '); 
        
        //alert(testVal);
        
//        Anthem_InvokeControlMethod(
//                userControlID,
//                'SimpleSearch',
//                [testVal], null );

        AddBackEntry('nv_home1' + guid ,userControlID+'!adv_search_click!'+guid,userControlID,guid);
        Anthem_InvokeControlMethod(
                userControlID,
                'advanceSearchForEnterButton',
                [testVal,guid], function(){
                    AdvSearch_HideSuggestList('SuggestiveSerarchResults');
                    RemoveTimers();
                    ClearCheckedItems();
                    Anthem_PostCallBack();}
                 );

        
        e                   = null;
        return false;
    }
    
    return true;        
   }
   
