// ************************************************
   function SetCookie(cookieName,cookieValue,nDays)
// ************************************************
 {
 var today = new Date();
 var expire = new Date();

 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
 }

// *******************************
   function ReadCookie(cookieName)
// *******************************
 {
 var theCookie=document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length;
 var sRes = unescape(theCookie.substring(ind+cookieName.length+1,ind1));
 if (sRes == ";") sRes = "";
 return sRes;
}


//==========================================================================
//This function has been removed as there is a Linker(s) function in the 
//ITB4_MainPage_Layout.xsl file.  That function has built in rules to extract
//the port number of the current location as well and will include the port
//number if it is a non-standard port (ie not port 80)
//
//By:	Werner Weber
//Date:	20/03/2007
//Ref:	Amanda Campbell
//==========================================================================
//function LinkerX(relativepath)
//// ====================================
//{
//var ServerName;
//ServerName = document.location.hostname;
//
//  securenum = relativepath.indexOf("s_caissa");
//  if (securenum >= 0) 
//  { 
//	len = relativepath.length;
//	relativepath = relativepath.substring(securenum+2,len);
//	document.location.href = "https://" + ServerName + "/" + relativepath;
//  }
//  else
//  {
//	document.location.href = "http://" + ServerName + "/" + relativepath;
//  }
//}	
//==========================================================================


function SetFormAction(relativepath, objForm)
// ====================================
{
var ServerName = document.location.hostname;
var strPath  = document.location.href
var aPath = new Array()
aPath = strPath.split('/')

var securenum = relativepath.indexOf("s_caissa");
var strLocation = ""

if (securenum >= 0){
	strLocation = "https:/"
	len = relativepath.length;
	relativepath = relativepath.substring(securenum+2,len);
}else{
	strLocation = "http:/"
}

for (var b=1;b<aPath.length-1;b++){
	if (aPath[b] != ''){
		strLocation = strLocation + '/' + aPath[b]
	}
}
strLocation = strLocation + '/' + relativepath;
objForm.action = strLocation
}


// *********************
// Check Required Fields
// *********************

   function setColor(el,bg)
// ========================
{if (el.style) el.style.backgroundColor = bg;}

  function SetSSL()
// =================
{
	//NOTE: the button must have name of "submit" no matter what it does
	var ServerName = document.location.hostname;	
	var strPath  = document.location.href
	var aPath = new Array()
	aPath = strPath.split('/')

	var strLocation = "https:/"
	var relativepath = "caissa2ASP.asp?Page=ITB_Home"

	for (var b=1;b<aPath.length-1;b++){
		if (aPath[b] != ''){
			strLocation = strLocation + '/' + aPath[b]
		}
	}
	strLocation = strLocation + '/' + relativepath;

	document.submit.action=strLocation;
}

   function UndoSSL()
// ==================
{
	var ServerName = document.location.hostname;	
	var strPath  = document.location.href
	var aPath = new Array()
	aPath = strPath.split('/')

	var strLocation = "http:/"
	var relativepath = "caissa2ASP.asp?Page=ITB_Home"

	for (var b=1;b<aPath.length-1;b++){
		if (aPath[b] != ''){
			strLocation = strLocation + '/' + aPath[b]
		}
	}
	strLocation = strLocation + '/' + relativepath;

	document.submit.action=strLocation;
}

   function checkinput(formname) 
// =====================
{
var bgBad = "#FEE7C5";
var bgGood = "white";
var sElement, sReq, sOK, sName, sID, sSSL;

sOK = true;
sSSL = false;
sBadGo = false;
//
//
//
//

z = eval('document.' + formname + '.elements.length');
//alert(z);

 for (var i=0; i < z; i++) 
  {
  sElement = eval('document.' + formname + '.elements[' + i + ']');

  sName=sElement.name;
  sName = sName.toUpperCase();

  
  //Kevin, if an element (button or hidden) was found on the page with its name matching the following then make secure.

  if (sName=="SUBMIT_CHECKOUT" || sName=="SUBMIT_CHECKOUT2")
   {
    sSSL = true;
   }

  sReq=sElement.name.substr(0,2);

  if (sReq == "x_")  
   { 												
   if (sElement.value == "") 
    { 											
    if (sOK) { sOK = false; sElement.focus(); }
    setColor(sElement, bgBad);
    }
   else
    {
    setColor(sElement, bgGood);
    }
   } 
  }
 

  
if (!sOK)
 {

 alert ("Please complete the fields before continuing.");

 
 return false;
 }
else
 {

/*

 if (sSSL) 
   {
   SetSSL();
   }
 else
   {
   UndoSSL();
   }

*/

 return true;
 }
}


function pl(p){
	var ServerName = document.location.hostname;
	var strPath  = document.location.href
	var aPath = new Array()
	aPath = strPath.split('/')
	
	var relativepath = "caissa.asp?Page=ITB4_RHProduct&Prod=" + p
	
	var strLocation = ""
	strLocation = "http:/"
	
	for (var b=1;b<aPath.length-1;b++){
		if (aPath[b] != ''){
			strLocation = strLocation + '/' + aPath[b]
		}
	}
	strLocation = strLocation + '/' + relativepath;	
	document.location.href = strLocation;
}

function CS_Popup(pge)
{
var PopEvent="caissa.asp?Page=ITB4_RHContext&Popup=Yes&Post=" + pge;
var desktop=window.open(PopEvent,"_blank","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=500,height=500");
}

function CS_PopupPlasticBags(pge)
{
var PopEvent="caissa.asp?Page=ITB4_RHContext&Popup=Yes&Post=" + pge;
var desktop=window.open(PopEvent,"_blank","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=700,height=500");
}

function CS_PopupNewTerms(pge)
{
var PopEvent="caissa.asp?Page=ITB4_RHTermsAndConditionsPopup&Popup=Yes&Post=" + pge + "&Submit=Decline";
var desktop=window.open(PopEvent,"_blank","toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width=700,height=500");
}

function stripChars(field)
{
	var i;
	var r = "";
	var s = field.value;
	var t = "1234567890";
	for (i = 0; i < s.length; i++) { if (t.indexOf(s.charAt(i)) != -1) r += s.charAt(i);}
	if (field.value != r) 
	{
		alert("This field can only contain numbers!");
		field.value = r;
	}
}

function stripSpecials(field)
{
	
	var s = field.value;
	var t = /[^a-z\A-Z\d- ]/i;

        if (t.test(s) == true)
	{
		alert("This field cannot contain these special characters!");
	}
	
}

function stripSpecialsOnSearch(field)
{
	var s = field.value;
	var t = /[\><&%]/i;
	var r = "";
	
	if (t.test(s) == true)
	{
		alert("This field cannot contain this special character!");
		field.value = r;
	}
}

//JAVASCRIPT FOR TEXTILE PRODUCT DETAIL
var aTextile = new Array();
var FILTER = "true";
var NORMAL = "false";
var sSelectedSKU = "0"
var ns4 = (document.layers) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;
var ie	= (document.all) ? true : false;
var sLowestPrice = "R0.00"
if (ns4) {docs_="document.layers.";}   
else if (ns6) {docs_="document.getElementById('";}
else if (ie){docs_="document.all.";}

function oTextile(inColour,inSize,inPrice,inSKU){
	this.Size = inSize;
	this.Colour = inColour;
	this.Price = inPrice;
	this.SKU = inSKU;
}		
function populateArrays(strText){
	var aCombo = strText.split("|");
	var aItem = new Array();
	var aSize = new Array();
	var sColour = "";
	var iLowestPrice = 999;
	var idx = 0
	for(i=0;i<aCombo.length;i++){
		if(aCombo[i] != ""){
			aItem = aCombo[i].split(";");
			sColour = aItem[0];
			for(c=1;c<aItem.length;c++){
				aSize = aItem[c].split("*");
				aTextile[idx] = new oTextile(sColour,aSize[0],aSize[1],aSize[2]);
				if (getPriceValue(aSize[1]) < iLowestPrice){
					iLowestPrice = getPriceValue(aSize[1]) 
				}
				else {
						iLowestPrice = getPriceValue(aSize[1])
					}
				idx++;
			}										
		}
	}
	sLowestPrice = "R " + iLowestPrice;
	populateColour(NORMAL);
	populateSize(NORMAL);
	populatePrice();	
	document.inputForm.ProdSize.selectedIndex = 0
	document.inputForm.ProdColour.selectedIndex = 0			
}
function getPriceValue(inValue){
	var aValue = new Array()
	aValue = inValue.split(" ")
	return parseFloat(aValue[2])
}

function populateColour(inFilter){
	var oldColour = ""
	var idx = 2
	document.inputForm.ProdColour.options.length = 0;	
	if(inFilter == FILTER) 	var sSize = document.inputForm.ProdSize[document.inputForm.ProdSize.selectedIndex].value	
	
	document.inputForm.ProdColour.options[0] = new Option("Available Colours","true");				
	document.inputForm.ProdColour.options[1] = new Option("--------------------","");		

	for(i=0;i<aTextile.length;i++){
		if(oldColour != aTextile[i].Colour && inFilter == NORMAL  && !ContainsValue(aTextile[i].Colour,document.inputForm.ProdColour) ){					
			document.inputForm.ProdColour.options[idx] = new Option(aTextile[i].Colour,aTextile[i].Colour);		
			idx++					
		}else{
			if(inFilter == FILTER && sSize != "" && sSize == aTextile[i].Size){
				document.inputForm.ProdColour.options[idx] = new Option(aTextile[i].Colour,aTextile[i].Colour);				
				idx++
			}
		}
		oldColour = aTextile[i].Colour;
	}		
}	
function filterColours(){
	var strSelect = document.inputForm.ProdSize[document.inputForm.ProdSize.selectedIndex].value
	if(document.inputForm.ProdColour[document.inputForm.ProdColour.selectedIndex].value == ""){
		document.inputForm.ProdColour.selectedIndex = 0
	}
	if(document.inputForm.ProdColour[document.inputForm.ProdColour.selectedIndex].value == FILTER){
		populateColour(NORMAL)	
		populateSize(NORMAL)
		document.inputForm.ProdSize.selectedIndex = 0			
	}else{
		populateSize(FILTER)
		checkSelect(strSelect,document.inputForm.ProdSize)	
	}
	populatePrice()
	setNSPosition() 
		
	if(document.inputForm.ProdSize.selectedIndex == 0 || document.inputForm.ProdColour.selectedIndex == 0){
		sSelectedSKU = "0"
	}
}
function filterSizes(){
	var strSelect = document.inputForm.ProdColour[document.inputForm.ProdColour.selectedIndex].value
	if(document.inputForm.ProdSize[document.inputForm.ProdSize.selectedIndex].value == ""){
		document.inputForm.ProdSize.selectedIndex = 0
	}
	if(document.inputForm.ProdSize[document.inputForm.ProdSize.selectedIndex].value == FILTER){
		populateColour(NORMAL)	
		document.inputForm.ProdColour.selectedIndex = 0
		populateSize(NORMAL)
	}else{
		populateColour(FILTER)
		checkSelect(strSelect,document.inputForm.ProdColour)
	}	
	populatePrice()		
	setNSPosition() 
	if(document.inputForm.ProdSize.selectedIndex == 0 || document.inputForm.ProdColour.selectedIndex == 0){
		sSelectedSKU = "0"
	}	
}
function populateSize(inFilter){		
	var oldSize = ""
	var idx = 2;
	var sColour = document.inputForm.ProdColour[document.inputForm.ProdColour.selectedIndex].value
	if(sColour != ""){
		document.inputForm.ProdSize.options.length = 0;	
		
		document.inputForm.ProdSize.options[0] = new Option("Available Sizes","true");				
		document.inputForm.ProdSize.options[1] = new Option("--------------------","");		
				
		for(i=0;i<aTextile.length;i++){
			if(inFilter == FILTER){
				if((sColour == aTextile[i].Colour) && (oldSize != aTextile[i].Size)){
					document.inputForm.ProdSize.options[idx] = new Option(aTextile[i].Size,aTextile[i].Size);				
					idx++
				}
			}else{
				if(oldSize != aTextile[i].Size && !ContainsValue(aTextile[i].Size,document.inputForm.ProdSize) ){
					document.inputForm.ProdSize.options[idx] = new Option(aTextile[i].Size,aTextile[i].Size);				
					idx++
				}
			}
			oldColour = aTextile[i].Colour;
		}	
	}
}	
function populatePrice(){
	if(document.inputForm.ProdSize.length > 0){
		var sPrice = ""
		var sSKU = ""
		var sFormatPrice = ""
		var sQuantity = ""
		var sColour = document.inputForm.ProdColour[document.inputForm.ProdColour.selectedIndex].value
		var sSize = document.inputForm.ProdSize[document.inputForm.ProdSize.selectedIndex].value
		if(eval('document.inputForm.Prod_' + sSelectedSKU  + '')){
			sQuantity  = eval('document.inputForm.Prod_' + sSelectedSKU  + '.value')
		}
		for(i=0;i<aTextile.length;i++){
			if(sSize == aTextile[i].Size && sColour == aTextile[i].Colour){
				sPrice = aTextile[i].Price
				sSKU =  aTextile[i].SKU				
				if(sSKU < 0) sSKU *= -1
				sSelectedSKU  = sSKU		
			}
		}	
		if(sPrice == ""){
			sPrice = sLowestPrice;	
		}
		if(sPrice == ""){
			if(ns4){
				writeDocument('ProdPrice','<font face="Helvetica,Arial,sans-serif" size=2><b>n/a</b></font>')
				writeDocument('SelectProdDetails','<form method="post"><input type="text" size="5" length="3" name="Prod_0" value="' + sQuantity  + '"/>' + '<input type="hidden" name="Price_0" value="0"/></form>')										
			}else{
				writeDocument('ProdPrice','n/a')
				writeDocument('SelectProdDetails','<input type="text" size="5" length="3" name="Prod_0" value="' + sQuantity  + '"/>' + '<input type="hidden" name="Price_0" value="0"/>')			
			}
			
		}else{
			for(p=1;p<sPrice.length;p++){
				if(parseInt(sPrice.substr(p,1)) != "NaN" && sPrice.substr(p,1) != " "){
					sFormatPrice  += sPrice.substr(p,1) 
				}										
			}			
			if(ns4){
				writeDocument('ProdPrice','<font face="Helvetica,Arial,sans-serif" size=2><b>' + sPrice + '</b></font>')
				writeDocument('SelectProdDetails','<form method="post"><input type="text" size="5" maxlength="3" name="Prod_' + sSKU + '" value="' + sQuantity   + '"/>' + '<input type="hidden" name="Price_' + sSKU + '" value="' + sFormatPrice*100  + '"/></form>')							
			}else {
				writeDocument('ProdPrice',sPrice)
				writeDocument('SelectProdDetails','<input type="text" size="5" maxlength="3" name="Prod_' + sSKU + '" value="' + sQuantity   + '"/>' + '<input type="hidden" name="Price_' + sSKU + '" value="' + sFormatPrice*100  + '"/>')			
			}
		}						
	}				
}		
function ContainsValue(strValue,oDropDown){
	var idx = 0
	var iMax = oDropDown.length
	while (idx < iMax){			
		if(oDropDown.options[idx].value == strValue){
			return true;
			break;			
		}
		idx++
	}
	return false;		
}	
function checkSelect(strValue, oDropDown){
	var idx = 0
	var iMax = oDropDown.length
	while (idx < iMax){			
		if(oDropDown.options[idx].value == strValue){
			oDropDown.options[idx].selected = true
			return true;
			break;			
		}
		idx++
	}
	oDropDown.options[2].selected = true
}		
function checkSKU(){
	if(document.inputForm.ProdColour){
		if(sSelectedSKU == 0){
			alert("Please select a colour and size")
			return false
		}else{
			if(ns4){
				if (eval('document.SelectProdDetails.document.forms[0].Prod_' + sSelectedSKU + '.value')== 0) {
					alert("Please fill in the required quantity")
					return false
				}else{
					document.SelectProdDetails.document.forms[0].submit()
				}
			}else{
				if(eval('document.inputForm.Prod_' + sSelectedSKU + '.value')==0){
					alert("Please fill in the required quantity")
					return false
				}else{
					document.inputForm.submit()		
				}
			}
			return false
		}				
	}else{
		return true
	}
}
function writeDocument(DivInner,goStr){
	if (ns4) {		
		eval('document.layers["' + DivInner + '"].document.write(goStr)');
        eval('document.layers["' + DivInner + '"].document.close()');        
    } else if (ie) {
        eval(docs_ + DivInner + '.innerHTML = goStr');
    } else if (ns6) {
        eval('document.getElementById("' + DivInner + '").innerHTML = goStr');
    }    
}

function setNSPosition() {
	/*
	if(ns4){
		document.ProdPrice.left = 495;
		document.ProdPrice.top = 197;		
		document.SelectProdDetails.left = 490;
		document.SelectProdDetails.top = 214;		
	}
	*/
}


function Set_RHComment_Subjects(a)
{
  var list = document.frm_Comment.Subject.options;
  while (list.length > 0) {list[0] = null;}
  list[0] = new Option("General comment");
  list[1] = new Option("Suggestions");
  list[2] = new Option("Compliments");
  list[3] = new Option("Please help/How do I...?");
  list[4] = new Option("Complaints");
  list[5] = new Option("Technical problem");
  if (a == "ITB") {
    list[6] = new Option("Order change/query");
    list[7] = new Option("Delivery query");
    list[8] = new Option("Payment query");
    list[9] = new Option("Returns/Refunds");
  }
  if (a == "WW") {
    list[6] = new Option("Product Query");
    list[7] = new Option("Store Query");
  }
  if (a == "WWFS") {
    list[6] = new Option("In Store Card");
    list[7] = new Option("Credit Card");
    list[8] = new Option("Unit trust");
    list[9] = new Option("Personal loans");
    list[10] = new Option("Registration");
    list[11] = new Option("PIN query");
  }
  list[list.length] = new Option("Policy Information");
  list[list.length] = new Option("Other");
}

function GeoLocate_CheckLocator2(){
			if (document.WebPart_GeoLocate) {
				var sURL = document.location.href
				var aURL = sURL.split("?")
			   var re = /ITB4_RHLogin|ITB4_RHRegister/i;           
			   var bInstr = aURL[1].search(re);  
				if(bInstr > 0){
					document.WebPart_GeoLocate.action = aURL[0] + "?Page=ITB4_RHContext&nbsp;Post=Home"
				}
				document.WebPart_GeoLocate.submit();
			}
		}

function GeoLocate_CheckLocator2(){
	if (document.WebPart_GeoLocate2) {
		var sURL = document.location.href
		var aURL = sURL.split("?")
		var re = /ITB_Login|ITB_Register/i;           
		var bInstr = aURL[1].search(re);  
		if(bInstr > 0){
			document.WebPart_GeoLocate2.action = aURL[0] + "?Page=ITB_Home"
			}
			document.WebPart_GeoLocate2.submit();
			}
		}