var _global = this;
var browser = navigator.appName;

//function to close step pop-up window
function closeThisStep(){
	if(this.window.opener && !this.window.opener.closed){
		this.window.close();
		window.opener.focus();
	}
	else{
		location='torders.cfm';
	}
}

//trim whitespace from start and end of any string.
function trim(strText){
	strText=strText.replace(/^\s+/, '');
	strText=strText.replace(/\s+$/, '');
	return strText;
}

//function to enable color selections for cust type
function showColorsSelector(){
	theChosen='';
	for (var i=0; i<document.custType.customizingType.length; i++){
		if(document.getElementById(document.custType.customizingType[i].value+'_Colors')){
			document.getElementById(document.custType.customizingType[i].value+'_Colors').style.display='none';
		}
		if (document.custType.customizingType[i].checked){
			theChosen=document.custType.customizingType[i].value;
		}
	}
	if(document.getElementById(theChosen+'_Colors')){
		document.getElementById(theChosen+'_Colors').style.display='block';
	}
}

//function to submit cust types
function typeSubmit(){
	//derrive cust type selected
	theChosen='Pro_Stitched';
	for (var i=0; i<document.custType.customizingType.length; i++){
		if (document.custType.customizingType[i].checked){
			theChosen=document.custType.customizingType[i].value;
		}
	}
	//derrive color numbers selected
	if(eval("document.custType."+theChosen+"_SelectColors")){
		SelIndex=eval("document.custType."+theChosen+"_SelectColors.selectedIndex");
		numColors=eval("document.custType."+theChosen+"_SelectColors.options["+SelIndex+"].value");
	}
	else{
		numColors="N/A";
	}
	if(eval("document.custType."+theChosen+"_SelectSleeveColors")){
		SelIndex=eval("document.custType."+theChosen+"_SelectSleeveColors.selectedIndex");
		numSleeveColors=eval("document.custType."+theChosen+"_SelectSleeveColors.options["+SelIndex+"].value");
	}
	else{
		numSleeveColors="N/A";
	}
	if(eval("document.custType."+theChosen+"_SelectNameColors")){
		SelIndex=eval("document.custType."+theChosen+"_SelectNameColors.selectedIndex");
		numNameColors=eval("document.custType."+theChosen+"_SelectNameColors.options["+SelIndex+"].value");
	}
	else{
		numNameColors="N/A";
	}
	//start asynch request to server
	if(browser == "Microsoft Internet Explorer") ro = new ActiveXObject("Microsoft.XMLHTTP");
	else ro = new XMLHttpRequest();
	ro.open('post', 'torders-s3-req.cfm');
	queryString='customizingType='+theChosen+'&numNameColors='+numNameColors+'&numSleeveColors='+numSleeveColors+'&numColors='+numColors+'&pageFunction=custType';
	ro.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	ro.send(queryString);
	ro.onreadystatechange = handleResponse;
}

//unsynchonous http request for cust type submission *******************************
var ro;
function handleResponse(){
	var response = trim(ro.responseText);
	if(ro.readyState == 4){
		if(ro.status == 200 && ro.responseText.indexOf('title06.gif') == -1){
			alert(response);
		}
		else{
			alert("We're sorry - an error ocurred.");
		}
	}
}
function changeCustType(newValue){//can be deleted after typeSubmit is working
	if(browser == "Microsoft Internet Explorer") ro = new ActiveXObject("Microsoft.XMLHTTP");
	else ro = new XMLHttpRequest();
	ro.open('post', 'torders-s3-req.cfm');
	queryString='customizingType='+newValue+'&pageFunction=custType';
	ro.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	ro.send(queryString);
	ro.onreadystatechange = handleResponse;
}

//unsynchonous http request for cust prefs submission *******************************
var prefRo;
function handlePrefsResponse(){
	var response = trim(prefRo.responseText);
	if(prefRo.readyState == 4){
		document.custOptions.custOptionsSubmit.value='Submit Customizing Choices';
		if(prefRo.status == 200 && prefRo.responseText.indexOf('title06.gif') == -1){
			alert(response);
		}
		else{
			alert("We're sorry - an error ocurred.");
		}
	}
}
function custPrefs(back, sleeve, front, name, button){
	button.value='Sending...';
	if(browser == "Microsoft Internet Explorer") prefRo = new ActiveXObject("Microsoft.XMLHTTP");
	else prefRo = new XMLHttpRequest();
	prefRo.open('post', 'torders-s3-req.cfm');
	queryString='backPref='+back+'&sleevePref='+sleeve+'&frontPref='+front+'&namePref='+name+'&pageFunction=custPrefs';
	prefRo.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	prefRo.send(queryString);
	prefRo.onreadystatechange = handlePrefsResponse;
}

//validate upload file type
function vFileType(fileVal, which){
	arrTemp=fileVal.split('.');
	theExt=arrTemp[arrTemp.length-1];
	if(fileVal.length > 0 && theExt != 'jpg' && theExt != 'gif' && theExt != 'jpeg' && theExt != 'tif' && theExt != 'tiff' && theExt != 'png' && theExt != 'pdf' && theExt != 'bmp' && theExt != 'ps' && theExt != 'ai' && theExt != 'cdr' && theExt != 'eps'){
		alert('Please upload a valid image file type.');
		return false;//change to false
	}
	else{
		showTheStatus(which);
		return true;
	}
}

//function to update/show a file upload form
function showTheForm(which,statusMsg,alertMsg){
	//define element references needed
	mainDiv=document.getElementById(which+'Div');
	resultTextDiv=document.getElementById(which+'ResultDiv');
	statusDiv=document.getElementById(which+'StatusDiv');
	//show appropriate messages and stop any progress updates
	clearInterval(_global[which+'ProgressInt']);
	alert(alertMsg);
	mainDiv.style.display='block';
	statusDiv.style.display='none';
	resultTextDiv.innerHTML=statusMsg;
}

//function to increment time ellapsed counter and update relevant div
function incrementCounter(which){
	_global[which+'CurrTime']++;
	cTime=_global[which+'CurrTime'];
	statusDiv=document.getElementById(which+'StatusDiv');
	theMin=Math.floor(cTime/60);
	theSec=cTime-(theMin*60);
	statusDiv.innerHTML="Uploading... Time Elapsed: <span style='font-weight:bold;'>[<span style='color:red;'>"+theMin+"</span>min: <span style='color:red;'>"+theSec+"</span>sec]</span><BR>Estimated time per megabyte: [Dial-up: 4min] [Broadband: 45sec]";
}

//function initialize counter div and start calling incrementCounter at Intervals
function showTheStatus(which){
	_global[which+'CurrTime']=0;
	statusDiv=document.getElementById(which+'StatusDiv');
	document.getElementById(which+'Div').style.display='none';
	statusDiv.style.display='block';
	statusDiv.innerHTML="Uploading... Time Elapsed:[-]<BR>Estimated time per megabyte: [Dial-up: 4min] [Broadband: 45sec]";
	_global[which+'ProgressInt']=setInterval("incrementCounter('"+which+"')",1000);
}