// Functions for the Stratco Website

function doPreviewRoofColour(sColour) {

	if (document.getElementById) {
		imgRoof = document.getElementById('previewroof');
		if (imgRoof)  {
			imgRoof.src = ("/nz/images/cgicolours/" + sColour + ".jpg");
		}
	}
}

/**
 * Swap the Displayed Image for a store location to either the map or a photo
 *
 */
function swapStoreImage(sIndex, bMap) {
	
	
	if (document.getElementById) {
		objDisplay = document.getElementById(sIndex+'display');
		
		//objBook = document.getElementById(sIndex +'book');
		//objPhoto = document.getElementById(sIndex +'photo');
	
		if (bMap) {
				// put map into image
				sURL = "/nz/images/stores/" + sIndex + ".jpg";
				
				//objBook.src = "/images/stores/frame/book_over.gif";
				//objPhoto.src = "/images/stores/frame/camera.gif";
		} else {
				// put photo into image
				sURL = "/nz/images/stores/photos/" + sIndex + ".jpg";
				//objBook.src = "/images/stores/frame/book.gif";		
				//objPhoto.src = "/images/stores/frame/camera_over.gif";				
		}
	
		objDisplay.src = sURL;
	}
	
	//bSwap = true;
}

//bSwap = false;

//function checkSwap() {
	
//	if(!bSwap) {
//		MM_swapImgRestore();
	//}
	//bSwap = false;
//}
/**
 * Display the application form for the Supersaver Card
 */
function doApplicationForm() {
	
	sURL = "/nz/stores/supersaverrequest.asp";
	
	launchURLSet(sURL,"560", "560" );
		
}

/**
 * Check the required fields have been entered for the Supersaver Application
 * and if so, then submit the form
 */ 
function doApplicationSubmit() {
	var FormErrors = 0; 
     var error_string = "";
     
     for(var i = 0; i < document.form1.length; i++) {
		// If any fields are missing, save fieldname in error_string and increment FormErrors
		if(document.form1[i].value == "" 
			&& document.form1[i].name != "Company" 
			&& document.form1[i].name != "gender" 
			&& document.form1[i].name != "DateOfBirth" 
			&& document.form1[i].name != "Mobile" 
			&& document.form1[i].name != "ABN" 
			&& document.form1[i].name != "Email"
			|| document.form1[i].value == "not_supplied")
		{
			error_string += document.form1[i].name + "\n";
			FormErrors++;
		}
	 }
     
	 if (FormErrors == 1) {
		alert("Please fill in the following field: \n\n" + error_string);
	 }
	 if (FormErrors > 1) {
		alert("Please fill in the following fields: \n\n" + error_string);
	 } 
	 if (FormErrors == 0) { 
         // SUBMIT FORM
         document.form1.submit(); 
     } // end if
	 
} // end SubmitFunction



function doSubmitBrochureRequest() {
	
		fm = document.BrochureRequestForm;
	msg="";
	newclass="";		
	sDIV="";
	
	if  (fm['strFirstName'].value=='') {
		msg+= sDIV + "First Name";
		sDIV=", ";
	newclass="INPUTrequired";
	} 
	fm['strFirstName'].className = newclass;
	newclass="";
	
	if  (fm['strLastName'].value=='') {
		msg+= sDIV + "Last Name";
		sDIV=", ";

		newclass="INPUTrequired";
	}
	fm['strLastName'].className = newclass;
	newclass="";		
	
	if  (fm['strStreetAddress'].value=='') {
		msg+= sDIV + "Street Address";
		sDIV=", ";
		newclass="INPUTrequired";
	}
	fm['strStreetAddress'].className = newclass;
	newclass="";		
	
	if  (fm['strSuburb'].value=='') {
		msg+= sDIV + "Suburb";
		sDIV=", ";

		newclass="INPUTrequired";
	}
	fm['strSuburb'].className = newclass;
	newclass="";			
	
	if  (fm['strState'][fm['strState'].selectedIndex].value =='') {
		msg+= sDIV + "State";
		sDIV=", ";

		newclass="INPUTrequired";		
	}
	if (document.getElementById) {
		document.getElementById('spanState').className = newclass;
	}
	newclass="";			
	
	if  (fm['strPostcode'].value=='') {
		msg+= sDIV + "Postcode";
		sDIV=", ";

		newclass="INPUTrequired";
	}
	fm['strPostcode'].className = newclass;
	newclass="";			
	
	if  (fm['strPhone'].value=='') {
		msg+= sDIV + "Phone Number";
		sDIV=", ";
		newclass="INPUTrequired";
	}
	fm['strPhone'].className = newclass;
	newclass="";			
	
	
	if (fm['strDescription'].value=='') {
		msg+= sDIV + "Brochure Description";
		sDIV =", ";
		newclass="INPUTrequired";		
	}
	fm['strDescription'].className = newclass;
	
	if (msg=='') {
			// can submit the form
			fm.submit();
			
	} else {
			
		alert('The Brochure Request cannot be submitted.\n\n' + msg + " are all required fields. \n\n These fields have been highlighted with a red border.");
		
	}
		

	
}
/**
 * Check the fields required for a Customer Quote have been entered and if so
 * submit the form
 */
function doSubmitGetAQuote() {
	
	fm = document.quoteForm;
	msg="";
	newclass="";		
	sDIV="";
	
	if  (fm['strFirstName'].value=='') {
		msg+= sDIV + "First Name";
		sDIV=", ";
	newclass="INPUTrequired";
	} 
	fm['strFirstName'].className = newclass;
	newclass="";
	
	if  (fm['strLastName'].value=='') {
		msg+= sDIV + "Last Name";
		sDIV=", ";

		newclass="INPUTrequired";
	}
	fm['strLastName'].className = newclass;
	newclass="";		
	
	if  (fm['strStreetAddress'].value=='') {
		msg+= sDIV + "Street Address";
		sDIV=", ";
		newclass="INPUTrequired";
	}
	fm['strStreetAddress'].className = newclass;
	newclass="";		
	
	if  (fm['strSuburb'].value=='') {
		msg+= sDIV + "Suburb";
		sDIV=", ";

		newclass="INPUTrequired";
	}
	fm['strSuburb'].className = newclass;
	newclass="";			
	
	if  (fm['strState'][fm['strState'].selectedIndex].value =='') {
		msg+= sDIV + "State";
		sDIV=", ";

		newclass="INPUTrequired";		
	}
	if (document.getElementById) {
		document.getElementById('spanState').className = newclass;
	}
	newclass="";			
	
	if  (fm['strPostcode'].value=='') {
		msg+= sDIV + "Postcode";
		sDIV=", ";

		newclass="INPUTrequired";
	}
	fm['strPostcode'].className = newclass;
	newclass="";			
	
	if  (fm['strPhone'].value=='') {
		msg+= sDIV + "Phone Number";
		sDIV=", ";
		newclass="INPUTrequired";
	}
	fm['strPhone'].className = newclass;
	newclass="";			
	
	if (  !(fm['chkSupply'][0].checked || fm['chkSupply'][1].checked)) {
		msg+= sDIV + "Quotation Type";
		sDIV =", ";
		newclass="INPUTrequired";
	}
	
	fm['chkSupply'][0].className = newclass;
	fm['chkSupply'][1].className = newclass;
	
	
	if (fm['strDescription'].value=='') {
		msg+= sDIV + "Quotation Description";
		sDIV =", ";
		newclass="INPUTrequired";		
	}
	fm['strDescription'].className = newclass;
	
	if (msg=='') {
			// can submit the form
			fm.submit();
			
	} else {
			
		alert('The Quote Request cannot be submitted.\n\n' + msg + " are all required fields. \n\n These fields have been highlighted with a red border.");
		
	}
		
	
	
	
	
	
	
}
function doPreviewFencingColour(sColour) {
	//alert(sColour);
	if (document.getElementById) {
		imgRoof = document.getElementById('previewfence');
		if (imgRoof)  {
			imgRoof.src = ("/nz/images/fencecolours/" + sColour + ".jpg");
		}
	}
	
}

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_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_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];}
}

function preloadImages() {
	// load images here
}

function storeFocus() {

	if (document.forms['StoreLocator'].postcode.value=='Enter Postcode/Suburb') {


		document.forms['StoreLocator'].postcode.value='';
		
	}
	
}

function storeSearch() {
	
	sPost = document.forms['StoreLocator'].postcode.value;
	sState="";
	
	
	if (document.forms['StoreLocator'].state.selectedIndex!=0) {

		sState = document.forms['StoreLocator'].state[document.forms['StoreLocator'].state.selectedIndex].text;
		
	}
	
	if (sPost=='' || sPost=='Enter Postcode/Suburb') {
		alert('Please Enter A Suburb or Postcode to Look For');
	} else {
	

		sURL = 'http://www.stratcodirect.com/storelocator/findStore.do?SAction=SEARCH&searchString=' + sPost  + '&State=' + sState;

		winX = window.open(	sURL,'storelocator','width=720,height=500,scrollbars=yes');	
		//window.location.href = sURL;		
	}

	return false;
	
}
function entsub(event,ourformName) {

	  if (event && event.which == 13) {
		  if (ourformName=='StoreLocator') {
			  storeSearch();
		  } else {
			  doSearchSubmit();
		  }
	  } else {
	  
		  if ( event.keyCode == 13) {
			  if (ourformName=='StoreLocator') {
				  storeSearch();
			  } else {
				  doSearchSubmit();
			  }
		} else {  

		    return true;
		}
	  }
}



function menuhighlighton(objX) {

	objX.className = 'menulinkRollover';

}

function doSearchSubmit()  {
	
	fmSearch = document.forms['frmsearch'];
	
	if (fmSearch['SearchString'].value!='') {
		
		fmSearch.submit();
	}	else {
		return false;
	}
}
function menuhighlightoff(objX, classname) {

if (menuhighlightoff.arguments[1]!=null ) {
	objX.className= classname;
} else {
objX.className = "menulink";
}
}

function doImagePreview(sProductID, bProductFirst) {

	sURL="/nz/showProductShots.asp?productid=" + sProductID +"&Related=" + bProductFirst;
	
	x = window.open(sURL, "preview", "width=600,height=650,status=no,left=10,top=10");

	x.focus();
}

function launchURL(sURL) {
		launchURLSet(sURL, "790", "800") ;
}

function launchURLSet (sURL, sWidth, sHeight) {

sFeatures="width=" + sWidth + ",height=" + sHeight + ",status=no,scrollbars=yes";

	x = window.open(sURL, "stratcoURL", sFeatures);
	
	x.focus();
	
}

iRowCount = 0;
function toggleCategory() {
	// check that browser supports getElementById
	if (!document.getElementById) {
		return false;
	}
	
	iRowCount=0;
	setTimeout('hideRow()', 20);
}

function hideRow() {

	objX = document.getElementById('toggle' + iRowCount);
	
	if (objX) {
		if (objX.style.display=='') {
			objX.style.display='none';
		} else {
			objX.style.display='';
		}
	
		iRowCount++;
	
		setTimeout('hideRow()', 20);
	} 

}


function showSuperSaverDirect() {
	
	x = window.open('https://www.stratcodirect.com/supersaver', 'supersaver','width=640,height=480');
	
	
}

// function to run when hovering over a nav button on the catalogue page
function doHoverNavButton(sButton) {
	
	document.body.style.cursor = 'hand';
	
	if (sButton=='prev') {
		document.getElementById('prevImage').src='/nz/images/buttons/over/previous_over.gif';
		
	} else if (sButton=='all') {
		document.getElementById('allImage').src='/nz/images/buttons/over/preview_all_over.gif';
		
	} else if (sButton=='next') {
		document.getElementById('nextImage').src='/nz/images/buttons/over/next_over.gif';
		
	}
	
}


// function to run when exitting a nav button on the catalogue page
function doExitNavButton(sButton) {
	document.body.style.cursor = 'default';
	
	if (sButton=='prev') {
		document.getElementById('prevImage').src='/nz/images/buttons/off/previous_off.gif';
		
	} else if (sButton=='all') {
		document.getElementById('allImage').src='/nz/images/buttons/off/preview_all_off.gif';
		
	} else if (sButton=='next') {
		document.getElementById('nextImage').src='/nz/images/buttons/off/next_off.gif';
		
	}
	
}


function doFlipToPage(offset) {

	var currPageNumber = +(document.getElementById('currentPageNumber').innerHTML);
	var newPage 	   = currPageNumber + Number(offset);
	var newPageID 	   = convertToPageID(newPage);
	
	//alert("~~~" + document.getElementById('currentPageNumber').innerHTML + "~~~");	
	
	if (document.getElementById('page' + newPageID)) { // page exists, open it
		doOpenPage(newPage);
	}
	
}


// function to run when hovering over a page selection cell on the catalogue page
function doHoverPageCell(pageID) {
	
	document.body.style.cursor = 'pointer';
	var cell = document.getElementById('page' + pageID);
	
	cell.style.border='solid 1px #CC0000';
	
}

// function to run when exitting a page selection cell on the catalogue page
function doExitPageCell(pageID) {
	
	document.body.style.cursor = 'default';
	var cell = document.getElementById('page' + pageID);
	
	cell.style.border='solid 1px #FFFFFF';
	
}

// the user has clicked a cell to open a page, run this function
function doOpenPage(selectedPage) {
	
	var index=0;
	var bContinue  = true;
	var cell, pageID;
	
	// first clear all cells
	while (bContinue) {
		
		index++;
		pageID = convertToPageID(index);
		cell = document.getElementById('page' + pageID);
		
		if (cell) {
			cell.innerHTML = index;
		} else {
			bContinue = false;
		}
		
	}
	
	// now highlight the selected cell
	cell = document.getElementById('page' + convertToPageID(selectedPage));
	cell.innerHTML=("<b><u>" + selectedPage + "</u></b>");
	
	// load in the selected page
	var imgPanel = document.getElementById('pagePreview');
	imgPanel.src = '/nz/catalogue/images/catalogue_' + convertToPageID(selectedPage) + '.png';
	
	// finally, set the new page ID
	document.getElementById('currentPageNumber').innerHTML = selectedPage;
	
}

function convertToPageID(iPage) {
	
	var pageID = '';
	
	if (iPage<10) {
		pageID = '0' + iPage;
	} else {
		pageID = iPage;
	}
	
	return pageID;
	
}