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;
}

//unsynchonous http request for extras prefs submission *******************************
var extrasRo;
function handlePrefsResponse(){
	var response = trim(extrasRo.responseText);
	if(extrasRo.readyState == 4){
		document.extrasOptions.extrasSubmit.value='Submit Accessory Choices';
		if(extrasRo.status == 200 && extrasRo.responseText.indexOf('title06.gif') == -1){
			alert(response);
		}
		else{
			alert("We're sorry - an error ocurred.");
		}
	}
}
function extrasPrefs(socks, helmet, button){
	button.value='Sending...';
	if(browser == "Microsoft Internet Explorer") extrasRo = new ActiveXObject("Microsoft.XMLHTTP");
	else extrasRo = new XMLHttpRequest();
	extrasRo.open('post', 'torders-s4-req.cfm');
	queryString='socksPref='+socks+'&helmetDecalsPref='+helmet+'&pageFunction=extrasPrefs';
	extrasRo.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    extrasRo.send(queryString);
	extrasRo.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);
}