/**
 * Optimum worksheet Javascript file 
 * @Author: Saul Yuan @ MMI
 * Created on Mar 28 2003
 */

// global variable to enable/disable alert box
var debug = "no";
var cookiePrefix = "c_";

var otherServer;
var thisLoc = location.toString();
if (thisLoc.indexOf('itl.') != -1) { // we are on ITL
	otherServer = 'http://itl.gm.ca';
} else if (thisLoc.indexOf('preprod.') != -1) { // we are on PREPROD
	otherServer = 'http://preprod.gm.ca';
} else if (thisLoc.indexOf('pprod.') != -1) { // we are on PREPROD apps
	otherServer = 'http://preprod.gm.ca';		
} else { // default to LIVE
	otherServer = 'http://gm.ca';	
}

function logger(msg) {
	if(debug == "yes")
		alert(msg);
}

function isCookieDisabled() {
	var tmpcookie = new Date();
	var chkcookie = tmpcookie.getTime() + '';
	document.cookie = "chkcookie=" + chkcookie + "; path=/";
	if (document.cookie.indexOf(chkcookie, 0) < 0)  {
		alert("Vous devez tout d'abord \" activer les fichiers cachés \" de votre navigateur pour utiliser cette feuille de travail.");
		return true;
	}	
	else 
		return false;
}
		
function getCookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) 
		return null;
	if (start == -1) 
		return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value) {
	document.cookie = name + "=" + escape(value) + ";path=/";
}

function deleteCookie (name, path, domain) { 
	if (getCookie(name)) { 
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; 
	} 
} 

// pull out only digits, return positive integer
function realInt(valueStr) {
	valueStr += " "
	var valueNum = "";
	for(var i=0; i<valueStr.length; i++) {
		var temp = valueStr.charAt(i);
		if(parseInt(temp) >= 0 ) valueNum += temp;
	}
	return (parseInt(valueNum) > 0)? parseInt(valueNum): 0;
}

function loadWorksheet(fm) {
	if(isCookieDisabled())
		return false;
		
	// form elements array
	var fmFields = document.forms[fm].elements;
	for(var j=0; j<fmFields.length; j++) {
			// check if cookie exists
			var fmElement = fmFields[j];
			var field = fmElement.name;
			var val = getCookie(cookiePrefix + field);
			logger("val is: " + val);
			
			if(val != null) {
				if(fmElement.type == "checkbox") {
					if(val == fmElement.value)
						fmElement.checked = true;
					else
						fmElement.checked = false;
				}
				else if(fmElement.type == "text") {
					// set cookie value to form field
					eval("document.forms[" + "'" + fm + "'" + "]." + field + ".value=" + '"' + val + '"');
				}
			}
	}
}

/**
 * @param fm Form name
 * @param validateFields validating form fields
 */
function updateWorksheet(fm, validateFields) {
	// validate form inputs
	if(!validateNumber(fm, validateFields)) {
		alert("Veuillez indiquer uniquement des numéros.");
		return;
	}
	
	// update cookie value
	var fmFields = document.forms[fm].elements;
	
	for(var i=0; i<fmFields.length; i++) {
		var fmElement = fmFields[i];
		var field = fmElement.name;
		var fmVal = fmElement.value;
		
		if(fmElement.type == "text")
			setCookie(cookiePrefix + field, fmVal);
		else if(fmElement.type == "checkbox") {
			if(fmElement.checked)
				setCookie(cookiePrefix + field, fmVal);
			else
				setCookie(cookiePrefix + field, "");
		}
	}
	alert("Mise à jour de la feuille de travail!");
	return;
}

function clearWorksheet(fm) {
	for(var i=0; i<worksheetFields.length; i++) {
		deleteCookie(cookiePrefix + worksheetFields[i], "/", "");
	}
	eval("document.forms[" + "'" + fm + "']" + ".reset()");
}

function printWorksheet(fm, validateFields) {
	updateWorksheet(fm, validateFields);
	
	window.open("print_preview.html", "printPreview");
	
	var inspection = getCookie(cookiePrefix + "");
	var testdrive = getCookie(cookiePrefix + "testdrive");
	if(inspection == "y" && testdrive == "y") {
	}
	else if(inspection == "y" && testdrive != "y") {
	}
	else if(inspection != "y" && testdrive == "y") {
	}
	else { // inspection != "y" && testdrive != "y")
	}

}

/**
 * @param fm Form name
 * @param validateFields validating form fields
 */
function validateNumber(fm, validateFields) {
	if(validateFields == null)
		return true;
		
	var invalidField = null;
	
	var fields = validateFields.split(",");
	for(var i=0; i<fields.length; i++) {
		var field = fields[i];
		var val = eval("document.forms[" + "'" + fm + "'" + "]." + field + ".value");

		var re = /[^\d,\.]/; // not digits, not ",", not "."
		if(re.test(val, "g")) {
			invalidField = field;
			break;
		}
	}
	
	if(invalidField != null) {
		eval("document.forms[" + "'" + fm + "'" + "]." + invalidField + ".focus()");
		return false;
	}
	
	return true;
}

/* start shandford additions 16/01/04 */

// same as updateWorksheet() - used to update before print preview
// only difference is no "Worksheet Updated" message
function updatePreview(fm, validateFields, isPrint) {
	// validate form inputs
	if(!validateNumber(fm, validateFields)) {
		alert("Veuillez indiquer uniquement des numéros.");
		return;
	}
	
	// update cookie value
	var fmFields = document.forms[fm].elements;
	
	for(var i=0; i<fmFields.length; i++) {
		var fmElement = fmFields[i];
		var field = fmElement.name;
		var fmVal = fmElement.value;
		
		if(fmElement.type == "text")
			setCookie(cookiePrefix + field, fmVal);
		else if(fmElement.type == "checkbox") {
			if(fmElement.checked)
				setCookie(cookiePrefix + field, fmVal);
			else
				setCookie(cookiePrefix + field, "");
		}
	}
	if(isPrint){
		javascript:var wsPrint=window.open('/static/french/used/worksheet_print.html','wsprint','width=620,height=500,screenX=25,screenY=190,left=25,top=190,menubar=yes,scrollbars=yes,resize=yes');
	}
	return;
}

// get a reference to the object
function getRef(id) {
	return (document.getElementById ? document.getElementById(id) : document.all[id]);
}

// opens the worksheet in a popup
function viewWorksheet() {
	openWin(otherServer+'/static/french/used/worksheet.html', '620', '500', 'yes', 'yes');
}

// opens the worksheet in a popup
function worksheetHelp() {
	var wshelp=window.open(otherServer+'/static/french/used/worksheet_help.html','wsHelp','width=400,height=250,screenX=25,screenY=190,left=25,top=190,toolbar=no,scrollbars=no,resize=no');
}

/* FUNCTION USED TO TARGET PARENT WINDOWS */
function openLink(file,target) {
	if(opener.closed){ // the parent window is closed
		var newWin = window.open(file, 'newUsedWin', 'height=550,width=770,screenX=25,left=25,screenY=150,top=150,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');
	} else { // target the parent
		target.window.location.href = file;
	}
}
