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 open/close message window div
function msg(vis){
	if(vis == 0) document.getElementById('messageWin').style.visibility="hidden";
	else if(vis == 1) document.getElementById('messageWin').style.visibility="visible";
	else document.getElementById('messageWin').innerHTML=vis;
}

//unsynchonous http request for crest 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 changeCrestType(newValue){
	if(browser == "Microsoft Internet Explorer") ro = new ActiveXObject("Microsoft.XMLHTTP");
	else ro = new XMLHttpRequest();
	ro.open('post', 'torders-s2-req.cfm');
	queryString='crestType='+newValue+'&pageFunction=crestType';
	ro.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	ro.send(queryString);
	ro.onreadystatechange = handleResponse;
}

//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,stockArtNum){
	//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;
	//change chosen stock art and display for it
	if(which=='crest'){
		if(document.getElementById('stockArtDiv_'+chosenStockArt)) document.getElementById('stockArtDiv_'+chosenStockArt).style.background=colorOut;
		_global.chosenStockArt=stockArtNum;
		if(document.getElementById('stockArtDiv_'+chosenStockArt)) document.getElementById('stockArtDiv_'+chosenStockArt).style.background=colorClick;
	}

}

//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);
}


//stockart interaction ******************
var colorClick='#faa45e';
var colorOut='#f8f8f8';
var colorOver='#e9e9e9';
var jerseyReq;

function artClickResponse(){
	if(jerseyReq.readyState == 2){
		msg("Receiving response...");
	}
	if(jerseyReq.readyState == 4){
		var response = trim(jerseyReq.responseText);
		if(jerseyReq.status == 200 && jerseyReq.responseText.indexOf('title06.gif') == -1 && jerseyReq.responseText.indexOf('torders error') == -1){
			msg("Your art selection has been recorded.");
			document.getElementById('crestTextArea').value=response;
			setTimeout("msg(0)",1250);
		}
		else if(jerseyReq.responseText.indexOf('torders error') != -1){
			msg("We're sorry. An error has occurred. Please try again later.\nCODE:"+response);
			setTimeout("msg(0)",10000);
		}
		else{
			msg("We're sorry - your choice could not be recorded due to an error. Please try again later.");
			setTimeout("msg(0)",10000);
		}
	}
}
function artClick(theId){
	msg(1);
	msg("Sending request...");
	//visual changes
	if(document.getElementById('stockArtDiv_'+chosenStockArt)) document.getElementById('stockArtDiv_'+chosenStockArt).style.background=colorOut;
	_global.chosenStockArt=theId.slice(12,theId.length);
	document.getElementById(theId).style.background=colorClick;
	//backend of jerseyclick
	if(browser == "Microsoft Internet Explorer") jerseyReq = new ActiveXObject("Microsoft.XMLHTTP");
	else jerseyReq = new XMLHttpRequest();
	jerseyReq.open('post', 'torders-s2-req.cfm');
	queryString='artId='+_global.chosenStockArt+'&pageFunction=recordStockArt';
	jerseyReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	jerseyReq.send(queryString);
	jerseyReq.onreadystatechange = artClickResponse;
}
