var ErrorList

function loadurl(dest) 
{
    try 
    {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } 
    
    catch (e) 
    {
    }

    xmlhttp.open("GET", dest, true);   
    xmlhttp.onreadystatechange = triggered;          
    xmlhttp.send(null);
}

function triggered() 
{
    var response = xmlhttp.responseText;
    var update = new Array();
                 
    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
    {

        if(response.indexOf('|' != -1)) 
        {
            update = response.split('|');
        }
                        
        document.getElementById(update[0]).innerHTML = update[1];        
    }
    
} 

function ValidateAddress() 
{
    ErrorList = '';

    var AttWhom = document.getElementById("txtAttWhom").value;        
    var AddLine1 = document.getElementById("txtAdd1").value; 
    var Suburb = document.getElementById("txtSuburb").value; 
    var Postal = document.getElementById("txtPostalCode").value; 
    
    if (AttWhom == '')
    {
        ErrorList = ErrorList + '<li>No Recipient Specified</li>'; 
    }
    
    if (AddLine1 == '')
    {
        ErrorList = ErrorList + '<li>No Address Specified</li>'; 
    }
        
    var emailPat = /^\d+$/;
    var matchArray = Postal.match(emailPat);
    
    if (matchArray == null)
    {                       
        ErrorList = ErrorList + '<li>Invalid Postal Code, Only Number Please!</li>';
    }       

            
    if (ErrorList != '')
    {
        window.location.hash="ErrorAnchor"; 
        document.getElementById('error').style.visibility = 'visible'; 
        document.getElementById('error').innerHTML = '<h3>Oops! There seems to be a problem!</h3><ul>' + ErrorList + '</ul>';        
        return false
    }
    else
    {
        document.getElementById('error').style.visibility = 'hidden'; 
    }   
}

function ValidateLogin() 
{
    ErrorList = '';
       
    var Email = document.getElementById("txtEmail").value; 
    
    ValidateEmail(Email);
    
    if (ErrorList != '')
    {
        window.location.hash="ErrorAnchor"; 
        document.getElementById('error').style.visibility = 'visible'; 
        document.getElementById('error').innerHTML = '<h3>Oops! There seems to be a problem!</h3><ul>' + ErrorList + '</ul>';        
        return false
    }
    else
    {
        document.getElementById('error').style.visibility = 'hidden'; 
    }    
} 

function ValidateDetailUpdate() 
{
    ErrorList = '';
    
    var Email = document.getElementById('txtEmail').value;
    var Firstname = document.getElementById('txtFirstname').value;  
    var Surname = document.getElementById('txtSurname').value;      
    var CellNo = document.getElementById('txtCellphone').value;          
    var Password1 = document.getElementById('txtPassword').value;
    var Password2 = document.getElementById('txtPasswordConfirm').value;
        
    ValidateEmail(Email);
    ValidateFirstName(Firstname);
    ValidateSurnameName(Surname)
    ValidateCellNo(CellNo);
    ValidatePassword(Password1, Password2);
    
    if (ErrorList != '')
    {
        document.getElementById('error').style.visibility = 'visible'; 
        document.getElementById('error').innerHTML = '<h3>Oops! There seems to be a problem!</h3><ul>' + ErrorList + '</ul>';
        return false
    }   
    else
    {
        document.getElementById('error').style.visibility = 'hidden'; 
    }
} 

function ValidateNewUser() 
{
    ErrorList = '';

    var Email = document.getElementById('txtEmail').value;
    var Firstname = document.getElementById('txtFirstname').value;  
    var Surname = document.getElementById('txtSurname').value;  
    var CellNo = document.getElementById('txtCellphone').value;      
    var Password1 = document.getElementById('txtPassword').value;
    var Password2 = document.getElementById('txtPasswordConfirm').value;
    var Terms = document.getElementById('terms');    

    ValidateEmail(Email)    
    ValidateFirstName(Firstname)
    ValidateSurnameName(Surname)
    ValidateCellNo(CellNo)    
    ValidatePassword(Password1, Password2)
    /*ValidateAge(document.getElementById("ddDay").value, document.getElementById("ddMonth").value, document.getElementById("ddYear").value)*/
    ValidateTerms(Terms)    

    if (ErrorList != '')
    {
        window.location.hash="ErrorAnchor"; 
        document.getElementById('error').style.visibility = 'visible'; 
        document.getElementById('error').innerHTML = '<h3>Oops! There seems to be a problem!</h3><ul>' + ErrorList + '</ul>'        
        return false
    }   
    else
    {
        document.getElementById('error').style.visibility = 'hidden'; 
    }    
} 

function ValidateEmail(Email)
{  
    if (Email == '')
    {
        Email = 'z';
    } 

    var emailPat = /^(([-\w$%&'*+\/=?^_`{|}~.]+)@(([-a-zA-Z0-9_]+\.)*)([-a-zA-Z0-9]+\.)([a-zA-Z0-9]{2,7}))?$/;
    var matchArray = Email.match(emailPat);
    
    if (matchArray == null)
    {                       
        ErrorList = ErrorList + '<li>Your Email address seems to be invalid</li>';
    }           
}     

function ValidatePassword(Password1, Password2)
{     
    if (Password1 == '')
    {
        ErrorList = ErrorList + '<li>Please choose a password!</li>';
        return
    }
    
    if (Password1.length < 6)
    {                       
        ErrorList = ErrorList + '<li>Your password is to short!</li>';
        return
    }    
       
    if (Password1 != Password2)
    {                       
        ErrorList = ErrorList + '<li>Your Passwords do not match!</li>';
    }           
}     

function ValidateAge(Day, Month, Year)
{
    NewYear = parseInt(Year)
    NewMonth = parseInt(Month)

    var myDate=new Date()
    myDate.setFullYear(NewYear+18, NewMonth-1, Day)

    var today = new Date()
       
    if (myDate>today)
    {
        ErrorList = ErrorList + '<li>You are not old enough to use WantItAll!</li>';
    }    
}

function ValidateFirstName(Firstname)
{     
    var NamePat = /[A-Za-z]/;
    var matchArray = Firstname.match(NamePat);

    if (matchArray == null)
    {
        ErrorList = ErrorList + '<li>Please type in a valid First Name!</li>';
    }
}

function ValidateSurnameName(Surname)
{     
    var NamePat = /[A-Za-z]/;
    var matchArray = Surname.match(NamePat);

    if (matchArray == null)
    {
        ErrorList = ErrorList + '<li>Please type in a valid Surname!</li>';
    }
}

function ValidateCellNo(CellNo)
{               
    var strReplaceAll = CellNo;
    var intIndexOfMatch = strReplaceAll.indexOf(" ");
  
    while (intIndexOfMatch != -1)
    {
        strReplaceAll = strReplaceAll.replace(" ", "");
        intIndexOfMatch = strReplaceAll.indexOf(" ");
    }
    
    CellNo = strReplaceAll
    document.getElementById('txtCellphone').value = CellNo
    
    var NamePat = /[0-9]/;
    var matchArray = CellNo.match(NamePat);
           
    if (matchArray == null || CellNo.length != 10 || CellNo.indexOf('0') != 0)
    {
        ErrorList = ErrorList + '<li>Please type in a valid Cellphone Number (only numbers please! eg. 0838361745)</li>';
    }
}

function ValidateTerms(Terms)
{
    if (Terms.checked == false)
    {
        ErrorList = ErrorList + '<li>Please read and accept the Terms and Conditions</li>';
    }
}

function HideWindow()
{
    parent.parent.GB_hide();
}

function ReloadParentOnClose()
{
    window.top.location.reload();
} 

function ToggleLayer(MyDiv)
{
    if (document.getElementById(MyDiv).style.visibility == 'hidden')
    {
        document.getElementById(MyDiv).style.visibility = 'visible'; 
    }
    else
    {
        document.getElementById(MyDiv).style.visibility = 'hidden'; 
    }  
}

function ChangeImage(NewLargeImage)
{
    //document.getElementById('LargeImage').innerHTML = LargeImage
    document['largeimage'].src = NewLargeImage;
}

function OpenWindow(MyImage)
{
    window.open(MyImage, null, "status=yes,height=550,width=550");
    //open(MyImage);
    //window.open("Sample.htm",null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");    
}

function swapImage(path) 
{
	document.getElementById("productImage").innerHTML = "<img id='largeimage' onclick='OpenWindow(this.src)' src=" + path + " />";
}

function checkPaymentMethod() 
{
	if (document.getElementById("eft").checked) 
	{
		document.getElementById("paymentMethodCC").style.display = "none"
		document.getElementById("paymentMethodEFT").style.display = ""
		document.getElementById("paymentMethodFNB").style.display = "none"
		document.getElementById("paymentMethodeBucks").style.display = "none"
		document.getElementById("paymentMethodUkash").style.display = "none"
	} 
    
    if (document.getElementById("mastercard").checked) 
    {
		document.getElementById("paymentMethodCC").style.display = ""
		document.getElementById("paymentMethodEFT").style.display = "none"   
		document.getElementById("paymentMethodFNB").style.display = "none"  
		document.getElementById("paymentMethodeBucks").style.display = "none"   
		document.getElementById("paymentMethodUkash").style.display = "none"
    }
    
    if (document.getElementById("visa").checked)
    {
		document.getElementById("paymentMethodCC").style.display = ""
		document.getElementById("paymentMethodEFT").style.display = "none"   
		document.getElementById("paymentMethodFNB").style.display = "none"
		document.getElementById("paymentMethodeBucks").style.display = "none"
		document.getElementById("paymentMethodUkash").style.display = "none"
    }
    
	if (document.getElementById("FNBCellPayPoint").checked)  
	{
		document.getElementById("paymentMethodCC").style.display = "none"
		document.getElementById("paymentMethodEFT").style.display = "none"
		document.getElementById("paymentMethodeBucks").style.display = "none"
		document.getElementById("paymentMethodFNB").style.display = ""
		document.getElementById("paymentMethodUkash").style.display = "none"
    }    
    
	if (document.getElementById("eBucks").checked)  
	{
		document.getElementById("paymentMethodCC").style.display = "none"
		document.getElementById("paymentMethodEFT").style.display = "none"
		document.getElementById("paymentMethodFNB").style.display = "none"
		document.getElementById("paymentMethodeBucks").style.display = ""
		document.getElementById("paymentMethodUkash").style.display = "none"
    }     
    
    if (document.getElementById("Ukash").checked)  
	{
		document.getElementById("paymentMethodCC").style.display = "none"
		document.getElementById("paymentMethodEFT").style.display = "none"
		document.getElementById("paymentMethodFNB").style.display = "none"
		document.getElementById("paymentMethodeBucks").style.display = "none"
		document.getElementById("paymentMethodUkash").style.display = ""
    }     
        
}

function refreshDelivery()
{
    $ajaxload('deliveryAddressDetails', '/AJAX.aspx?param1=deliveryaddress', '<div class=\'loadingcart\'>Loading Cart</div>', 'highlight', false);
}

function refreshTotals()
{
    $ajaxload('yourTotalDetails', '/AJAX.aspx?param1=ordertotal', '<div class=\'loadingcart\'>Loading Cart</div>', 'highlight', false);
}

function applyCoupon()
{
    $ajaxload('yourTotalDetails', '/AJAX.aspx?param1=ordertotal&param2=' + document.getElementById("txtCouponCode").value  + '', '<div class=\'loadingcart\'>Loading Cart</div>', 'highlight', false);
}

function showFeaturedProducts()
{
    $ajaxload('featuredProducts', '/AJAX.aspx?param1=featuredproducts', '<div class=\'loadingcart\'>Loading Cart</div>', 'highlight', false);
}

function startGallery() {
    var myGallery = new gallery($('myGallery'), {
        timed: true,
        delay: 6000,
        showInfopane: false,
        defaultTransition: "fadeslideleft"
    });
}

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 ValidateRAFEmails() 
{
    $ajaxload('friend1', '/AJAX.aspx?param1=raf&param2=' + document.getElementById("txtFriend1").value + ';' + document.getElementById("txtFriend1Name").value + '', '<div class=\'checkEmail\'>Validating Email</div>', 'highlight', false);
    $ajaxload('friend2', '/AJAX.aspx?param1=raf&param2=' + document.getElementById("txtFriend2").value + ';' + document.getElementById("txtFriend2Name").value + '', '<div class=\'checkEmail\'>Validating Email</div>', 'highlight', false);
    $ajaxload('friend3', '/AJAX.aspx?param1=raf&param2=' + document.getElementById("txtFriend3").value + ';' + document.getElementById("txtFriend3Name").value + '', '<div class=\'checkEmail\'>Validating Email</div>', 'highlight', false);
    $ajaxload('friend4', '/AJAX.aspx?param1=raf&param2=' + document.getElementById("txtFriend4").value + ';' + document.getElementById("txtFriend4Name").value + '', '<div class=\'checkEmail\'>Validating Email</div>', 'highlight', false);
    $ajaxload('friend5', '/AJAX.aspx?param1=raf&param2=' + document.getElementById("txtFriend5").value + ';' + document.getElementById("txtFriend5Name").value + '', '<div class=\'checkEmail\'>Validating Email</div>', 'highlight', false);
}

function RAFSaveEmails() 
{
    var friend1 = document.getElementById("friend1").innerHTML;
    var friend2 = document.getElementById("friend2").innerHTML;
    var friend3 = document.getElementById("friend3").innerHTML;
    var friend4 = document.getElementById("friend4").innerHTML;
    var friend5 = document.getElementById("friend5").innerHTML;

    if ((friend1.search("OK!") != -1) && (friend2.search("OK!") != -1) && (friend3.search("OK!") != -1) && (friend4.search("OK!") != -1) && (friend5.search("OK!") != -1))
    {
        $ajaxload('saveEmails', '/AJAX.aspx?param1=rafsave&param2=' + document.getElementById("txtFriend1").value + ";" + document.getElementById("txtFriend1Name").value + ';' + document.getElementById("txtFriend2").value + ";" + document.getElementById("txtFriend2Name").value + ';' + document.getElementById("txtFriend3").value + ";" + document.getElementById("txtFriend3Name").value + ';' + document.getElementById("txtFriend4").value + ";" + document.getElementById("txtFriend4Name").value + ';' + document.getElementById("txtFriend5").value + ";" + document.getElementById("txtFriend5Name").value + ";" + document.getElementById("txtYou").value + ";" + document.getElementById("txtYourName").value + '', '<div class=\'checkEmail\'>Checking If The Email Address Already Exists</div>', 'highlight', false);
        
        document.getElementById("txtFriend1").value = '';
        document.getElementById("txtFriend2").value = '';
        document.getElementById("txtFriend3").value = '';
        document.getElementById("txtFriend4").value = '';
        document.getElementById("txtFriend5").value = '';
        
        document.getElementById("friend1").innerHTML = '';
        document.getElementById("friend2").innerHTML = '';
        document.getElementById("friend3").innerHTML = '';
        document.getElementById("friend4").innerHTML = '';
        document.getElementById("friend5").innerHTML = '';
        
        document.getElementById("txtFriend1Name").value = '';
        document.getElementById("txtFriend2Name").value = '';
        document.getElementById("txtFriend3Name").value = '';
        document.getElementById("txtFriend4Name").value = '';
        document.getElementById("txtFriend5Name").value = '';        
    }
    else
    {
        document.getElementById("saveEmails").innerHTML = '<br /><font color=""red""><b>Please Validate Email Addresses Before Proceeding!</b></font><br />';
    }
    
}

function Social_Load() 
{ 
    var d=document; if(d.images){ if(!d.Social) d.Social=new Array();
    var i,j=d.Social.length,a=Social_Load.arguments; for(i=0; i<a.length; i++)
    
    if (a[i].indexOf("#")!=0){ d.Social[j]=new Image; d.Social[j++].src=a[i];}}
}

function schnipp() 
{ 
    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 schnupp(n, d) 
{ 
  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=schnupp(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function schnapp() 
{ 
  var i,j=0,x,a=schnapp.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=schnupp(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function getsuburbs()
{
    $ajaxload('suburb', '/AJAX.aspx?param1=addresssuburb&param2=' + document.getElementById("txtPostalCode").value, '<div class=\'loading\'>Fetching Suburbs</div>', 'highlight', false);
}

function getCustomerReviews()
{
    $ajaxload('reviews', '/AJAX.aspx?param1=customerreviews', '<div class=\'loading\'>Fetching Reviews</div>', 'highlight', false);
}

function additemtocart(itemno)
{
    $ajaxload('myvisshopcart', url = '/AJAX.aspx?ItemNumber=' + itemno + '&Qty=1', '<div class=\'loadingcart\'>Loading Cart</div>', 'highlight', false);
}

//function refreshprodpromotiontext() {
//    $ajaxload('promotion', url = '/AJAX.aspx?operation=promotionrefresh', 'Loading', false, false);
//}

function additemtowishlist(itemno) {
    $ajaxload(false, url = '/AJAX.aspx?Wishlist=True&ItemNumber=' + itemno + '&Qty=1', false, false, false);
}

function FilterResults() 
{
    $ajaxload('categoryitems', url = '/AJAX.aspx?Search=' + document.getElementById("txtSearchFilter").value + '&Sort=' + document.getElementById("cmbSort").value + '', '<div class=\'loadingcart\'>Loading Items</div>', 'highlight', false);
}
function searchSuggest(searchtext) {
    $ajaxload('searchsuggestcontainer', url = '/AJAX.aspx?Operation=Suggest&SearchText=' + searchtext, '', false, false);
}
function searchUseSuggest(searchtext) {
    document.getElementById('txtSearch').value = searchtext;
    document.getElementById('searchsuggest').style.display = 'none';
}
function searchLoseFocus() {
    var t = setTimeout('document.getElementById("searchsuggest").style.display = \'none\';', 250);
}
function productGetSimilar(asin) {
    $ajaxload('divsimilar', '/AJAX.aspx?Operation=GetSimilar&ItemNumber=' + asin, '<div class=\'loading\'>Fetching Similar Items</div>', false, false);
}
function productGetAccessories(asin) {
    $ajaxload('divaccessories', '/AJAX.aspx?Operation=GetAccessories&ItemNumber=' + asin, '<div class=\'loading\'>Fetching Accessories</div>', false, false);
}