﻿function PreLoadImages()
{
    MM_preloadImages('images/common/join_now_btn_ovr.png','images/common/search_btn_ovr.png','images/common/privacy_policy_btn_ovr.png','images/common/terms_condtion_btn_ovr.png','images/common/careers_btn_ovr.png','images/common/site_map_btn_ovr.png','images/common/tell_a_friend_btn_ovr.png','images/common/add_to_favourites_btn_ovr.png','images/common/myacc_login_btn_ovr.png','images/common/my_hanger_btn_ovr.png');
}

function doSearch()
{
    if (event.which || event.keyCode)
    {
        if ((event.which == 13) || (event.keyCode == 13)) 
        { 
            var oButt = document.getElementById("ucMid_imbSearch");
            
            event.returnValue = false;
            event.cancel = true;
            oButt.click();
        }    
    }
}

function ValidateSearch()
{
    var oValue = document.getElementById('ucTopRight_txtSearch').value;
    
    if (Trim(oValue) != '')
    {
        return true;
    }
    else
    {
        alert("Please enter a keyword to search.");
        
        return false;
    }
}

function OnClickClear(oItem, sDefault)
{
    var oItem = document.getElementById(oItem);
    
    if (trim(oItem.value) == sDefault)
    {
        oItem.value = '';
    }        
}

function OnBlurCheck(oItem, sDefault)
{
    var oItem = document.getElementById(oItem);
    
    if (trim(oItem.value) == '')
    {
        oItem.value = sDefault;
    }
}

function ValidateSubscription()
{
    var oForm = document.forms[0];
    
    var dName = document.getElementById("CustomFields[6]");
    
    var MandatoryString, bValidEmail;
    
    bValidEmail = false;
    
    MandatoryString = "CustomFields[6]~email";
    
    if (trim(dName.value) != 'Name' && (trim(oForm.email.value) != 'E-mail'))
    {
        if (CheckMandatory(MandatoryString) == true)
        {
            if (emailcheck(oForm.email.value) == true)
                bValidEmail = true;
        }
    }
    else
        alert('Please enter a valid Name & an E-mail address.');
    
    return bValidEmail;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//------------------------------------------------------------------------------------
//Validation for mandatory fields
//------------------------------------------------------------------------------------
function CheckMandatory(MandatoryString)
{
	var Completed = true;
	var sPrefix, oEle;
	
	MandatoryArray = MandatoryString.split("~");	
	
	form_elements:
	for (var i=0; i < MandatoryArray.length; i++)
	{
		sPrefix = MandatoryArray[i].substr(0, 3);
		oEle = document.getElementById(MandatoryArray[i]);
		
		switch (sPrefix)
		{
			case "txt":	if (oEle.value == ''){Completed = false; break form_elements;}break;				
			case "cmb":	if (oEle.selectedIndex <= 0){Completed = false; break form_elements;}break;
			case "ls":	if (oEle.selectedIndex <= 0){Completed = false; break form_elements;}break;
		}
	}

	if (Completed == false)
	{
		alert ("Please give information marked as mandatory(*)!");
		return false;
	}
	else
		return true;
}

//------------------------------------------------------------------------------------
//Validation for email addresses
//------------------------------------------------------------------------------------
function emailcheck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert("Invalid E-mail address");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Invalid E-mail address");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address");
		    return false;
		 }

 		 return true;				
}

//--------------------------
// Check Special Character
//--------------------------
function CheckSpecialCharacter(sFormName, sSPCValidation) {

	var bRetVal = false;
	var oElements;
	var sRegPat = /'+/;
	var arrElementName = sSPCValidation.split("~");
	
	for (var i = 0; i < arrElementName.length; i++)
	{
		oElements = eval(sFormName + "." + arrElementName[i]);
		
		//alert(oElements.value);
	
		if (TestSpecialCharExp(oElements.value) == true)
		{
			oElements.focus();
			return true;
		}
	}
	
	if (bRetVal == false)
		return false;
}

//------------------------------------------
// Check Special Character, for single field
//------------------------------------------
function TestSpecialCharExp(sInputString) {

	var sRegPat = /'+/;
	
	if (sRegPat.test(sInputString))
	{
		alert("Contains an invalid character (\'), Please remove it and resubmit your data");
		return true;
	}
	else
		return false;
}

function DisplayBigImage(sSrc)
{
    var oItem = document.getElementById('imgPrd');
    
    oItem.src = 'images/products/big/' + sSrc;
}

function GetSize()
{
    var nSize; 
    
    if (document.frmMain.cmbSize != undefined)
    {
        if (document.frmMain.cmbSize.selectedIndex > 0) 
            nSize = document.frmMain.cmbSize.options[document.frmMain.cmbSize.selectedIndex].value;
    }
    else 
        nSize = 1; 

    return nSize;
}

function ValidateAvailability(nPrdId)
{
    var nColour, nSize, nQty; 
    var bRetVal = false; var sMsg = ""; 
    
    nSize = GetSize(); 
    
    if ((document.frmMain.cmbColour.selectedIndex > 0) && (parseInt(nSize) > 0))
    {
        nQty = document.frmMain.txtQty.value;
        
        if (!isNaN(nQty))
        {
            if (nQty > 0)
            {
                nColour = document.frmMain.cmbColour.options[document.frmMain.cmbColour.selectedIndex].value; 
    
                var nAvailability = parseInt(product.CheckStockStatus(nPrdId, nColour, nSize).value); 
    
                DisplayAvailability(nAvailability); 
                
                if (nAvailability == 1)
                {
                    bRetVal = product.CheckQty(nPrdId, nColour, nSize, nQty).value; 
                    
                    if (bRetVal == false) 
                        sMsg = "Sorry, we are unable to provide the quantity requested!";
                }
                else 
                    sMsg = "Sorry, this product is out of stock at the moment, please check back later!";
            }
            else 
                sMsg = "Please enter valid Qty!";
        }
        else 
            sMsg = "Please enter valid Qty!";
    }
    else 
        sMsg = (nSize == 1)? "Please select colour!" : "Please select colour/size!"; 
    
    document.getElementById('dvQtyMsg').innerHTML = sMsg; 
    
    return bRetVal;
}

function DisplayAvailability(nStatus)
{
    var sStkStatus; 
    
    switch (nStatus)
    {
        case 1:sStkStatus = "<img src='images/common/available_icn.png' alt='Available' />"; break; 
        case 2: sStkStatus = "<img src='images/common/comming_soon_icn.png' alt='Coming soon' />"; break; 
        case 3:sStkStatus = "<img src='images/common/unavailable_icn.png' alt='Unavailable' />"; break;
    }
    document.getElementById('dvSIN').innerHTML = sStkStatus;
}

function CheckAvailability(nPrdId)
{
    var nColour, nSize; 
    
    nSize = GetSize(); 
    
    document.getElementById('dvSIN').innerHTML = "?";	
    
    if ((document.frmMain.cmbColour.selectedIndex > 0) && (parseInt(nSize) > 0))
    {
        document.getElementById('dvQtyMsg').innerHTML = ""; 
        
        nColour = document.frmMain.cmbColour.options[document.frmMain.cmbColour.selectedIndex].value; 
        
        DisplayAvailability(parseInt(product.CheckStockStatus(nPrdId, nColour, nSize).value));
    }
}

function getImages(oColour, nProdId)
{
    var oImage = document.getElementById("imgPrd");

    if (oColour.selectedIndex > 0)
    {
        var sImg = product.DisplayImages(nProdId, oColour.options[oColour.selectedIndex].value).value; 
        
        oImage.src = sImg;
    }
}
