// JM: Get the location of pages, irrespective of environment
var strSiteRoot = "http://www.renishaw.com/";
var oLoc = document.location;
if (oLoc.protocol.substring(0,4) == "http") strSiteRoot = oLoc.protocol + "//" + oLoc.host;


// NEW WINDOWS

function DocWindow(doc) {
	window.open(cleanURL(doc), '', 'width=682,height=500,left=55,top=45,toolbar=no,directories=no,location=no,status=yes,scrollbars=yes,resizable=yes');
}

function AccWindow(doc) {
	var NewAcc = window.open(cleanURL(doc), 'AccessoryWindow', 'width=450,height=400,left=55,top=45,toolbar=no,directories=no,location=no,status=yes,scrollbars=yes,resizable=yes');
	NewAcc.refresh();
	NewAcc.clear();
}

function TipWindow(doc) {
	window.open(cleanURL(doc), '', 'width=400,height=300,left=200,top=200,toolbar=no,directories=no,location=no,status=no,scrollbars=yes,resizable=yes');
}


// GLOSSARY

function GlossWindow(doc) {
	window.open(cleanURL(doc), '', 'width=300,height=400,left=200,top=50,toolbar=no,directories=no,location=no,status=no,scrollbars=yes,resizable=yes');
}


function OpenGlossary() {
	// Get the language
	var strLang = (arguments.length == 0) ? "UKEnglish" : arguments[0];
	
	GlossWindow('/client/publicity/' + strLang + '/glossary.html');
}

function OpenGlossaryUKEnglish() {
	GlossWindow('/client/publicity/UKEnglish/glossary.html');
}



/* JM: 15/03/04: This is a legacy function (I think)
function OpenGlossaryUKEnglishpref() {
	GlossWindow('/client/publicity/UKEnglish/glossary.html');
}

function OpenGlossaryFrench() {
	GlossWindow('/client/publicity/French/glossary.html');
}
function OpenGlossaryGerman() {
	GlossWindow('/client/publicity/German/glossary.html');
}
function OpenGlossaryItalian() {
	GlossWindow('/client/publicity/Italian/glossary.html');
}
*/




// RFQ/SAR/JOB/ etc

function UpdateQuote(ID) {
	window.open(strSiteRoot + '/client/asp/UpdateQuote.asp?quoteadd='+ID, '', 
	 'width=200,height=200,left=200,top=200,toolbar=no,directories=no,' + 
	 'location=no,status=no,scrollbars=yes,resizable=no');
}

function OpenRFQ() {
	// Get the language
	var strLang = (arguments.length == 0) ? "UKEnglish" : arguments[0];
	var strURL = strSiteRoot + "/client/asp/requestforquote1.asp?Lang=" + strLang

/*
	// Get the language
	if (arguments.length == 0) {
	} else {
		var strURL = strSiteRoot + "/client/asp/requestforquote1_" + arguments[0] + ".asp"
	}
*/

	var QuoteWindow = window.open(strURL,'', 'width=680,height=460,left=55,top=45,' +
	 'toolbar=no,directories=no,location=no,status=yes,scrollbars=yes,resizeable=yes');
}

function OpenRFQpref() {
	var QuoteWindow = window.open(strSiteRoot + "/client/asp/requestforquote1.asp",'',
	 'width=680,height=460,left=55,top=45,toolbar=no,directories=no,' +
	 'location=no,status=yes,scrollbars=yes,resizeable=yes');
}

function OpenSendRequest() {
	DocWindow('/client/asp/send-a-request-region.asp?id=366');
}

function OpenContactWebMaster() {
	var strURLParams = "";

	// Get the language
	if (arguments.length != 0) strURLParams = "&Lang=" + arguments[0];


	DocWindow('/client/asp/contactwebmaster-region.asp?id=366' + strURLParams);
}

function OpenJobApplication() {
	var strURLParams = "";

	// Get the language
	if (arguments.length != 0) strURLParams = "&Lang=" + arguments[0];

	DocWindow('/client/asp/jobapplication.asp?id=446' + strURLParams);
}

function OpenSpecInfoPack() {
	var strURLParams = "";

	// Get the language
	if (arguments.length != 0) strURLParams = "?Lang=" + arguments[0];

	DocWindow('/client/asp/request-infopack-region.asp' + strURLParams);
}

function OpenMCG() {
	var strURLParams = "";

	// Get the language
	if (arguments.length != 0) strURLParams = "&Lang=" + arguments[0];

	DocWindow('/client/asp/mcg-userdetails-region.asp?id=669' + strURLParams);
}

function OpenCalRegistartion() {
	DocWindow('/client/asp/CALRegistration.asp');
}


// JM: Clean a URL into a full and valid one
function cleanURL (sURL) {
	var strCleanURL = sURL;
	
	// remove any bits prior to a /
	while (strCleanURL.substring(0,1) != "/") {
		strCleanURL = strCleanURL.substring(1);
	}


	// remove any more relative navigation
	while (strCleanURL.substring(0,3) == "/..") {
		strCleanURL = strCleanURL.substring(4);
	}


	// Add in the /client root if it is in the asp subfolder
	if (strCleanURL.substring(0,4) == "/asp") strCleanURL = "/client" + strCleanURL;


	return strSiteRoot + strCleanURL;
}


// EPC 

function EPCQuoteBasket(doc, name) {
	window.open(doc, name, 'width=500,height=550,left=0,top=0,toolbar=no,directories=no,' +
	 'location=no,status=yes,scrollbars=yes,resizable=no');
}

function EPCQuoteForm(doc, name) {
	window.open(doc, name, 'width=500,height=550,left=50,top=50,toolbar=no,directories=no,' +
	 'location=no,status=yes,scrollbars=yes,resizable=no');
}

function OpenEPC() {
	EpcWindow('/client/asp/EPC-config.asp?id=674');
//	EpcWindow('/client/asp/EPC-types.asp?id=1160');		// Offer magnetic encoders too...
}

function EpcWindow(doc) {
	window.open(doc, '', 'width=750,height=550,left=25,top=25,toolbar=no,directories=no,' +
	 'location=no,status=yes,scrollbars=yes,resizable=yes');
}


// DISCLAIMER 

function OpenDisclaimerUKEnglish() {
	DocWindow('/client/includecustom/disclaimerUKEnglish.html');
}
function OpenDisclaimerGerman() {
	DocWindow('/client/includecustom/disclaimerGerman.html');
}
function OpenDisclaimerFrench() {
	DocWindow('/client/includecustom/disclaimerFrench.html');
}
function OpenDisclaimerItalian() {
	DocWindow('/client/includecustom/disclaimerItalian.html');
}
function OpenDisclaimerSpanish() {
	DocWindow('/client/includecustom/disclaimerSpanish.html');
}


// SPECIAL NEEDS 

function OpenSpecialNeedsUKEnglish() {
	DocWindow('/client/includecustom/specialneedsUKEnglish.html');
}
function OpenSpecialNeedsFrench() {
	DocWindow('/client/includecustom/specialneedsFrench.html');
}
function OpenSpecialNeedsGerman() {
	DocWindow('/client/includecustom/specialneedsGerman.html');
}
function OpenSpecialNeedsItalian() {
	DocWindow('/client/includecustom/specialneedsItalian.html');
}
function OpenSpecialNeedsSpanish() {
	DocWindow('/client/includecustom/specialneedsSpanish.html');
}


// BROWSER INFO

function OpenBrowserInfoUKEnglish() {
	DocWindow('/client/includecustom/browserinfoUKEnglish.html');
}
function OpenBrowserInfoGerman() {
	DocWindow('/client/includecustom/browserinfoGerman.html');
}
function OpenBrowserInfoFrench() {
	DocWindow('/client/includecustom/browserinfoFrench.html');
}
function OpenBrowserInfoItalian() {
	DocWindow('/client/includecustom/browserinfoItalian.html');
}
function OpenBrowserInfoSpanish() {
	DocWindow('/client/includecustom/browserinfoSpanish.html');
}


// CHECKBOX FUNCTIONS

function GetCheckBoxes(FormName) {
	var inc;
	var IDString = "";
	var element;

	for ( inc = 0; inc < document.StylusList.length; inc++ ) {
		element = document.StylusList.elements[inc];

		if ( element.type == "checkbox" ) {
			if ( element.checked == true ) {
				if ( IDString == "" ) {
					IDString = element.value;
				} else	{
					IDString = IDString + "," + element.value;
				}
			}
		}
	}

	if ( IDString !== "" ) {
		UpdateQuote(IDString);
	} else {
		alert("Please select which products you wish to add to your quotation list.\n\nSelezionare i prodotti che si vogliono aggiungere alla richiesta d'offerta\n\nBitte wahlen Sie die Produkte fur das Angebot");
	}
}


// COMPARISON FUNCTIONS 

function ChangeProduct(WhichSide) {
	var SelectBox = document.all[WhichSide+"ProductSelect"];
	var ShowProductID = document.all[WhichSide+"ProductSelect"].options[document.all[WhichSide+"ProductSelect"].selectedIndex].value;

	for (var i = 0; i < SelectBox.length; i++) {
		var val = SelectBox.options[i].value;
		document.all[WhichSide+"SpecDiv"+val].style.display = "none";
	}

	document.all[WhichSide+"SpecDiv"+ShowProductID].style.display = "";
}


// STYLESHEET CHANGING FUNCTIONS

var StyleSheet = ck_getCookie("Style");


if (StyleSheet == "Style") {
	which_style = "/client/stylesheets/Style.css";

} else if (StyleSheet == "Style9") {
	which_style = "/client/stylesheets/Style9.css";

} else if (StyleSheet == "Style10") {
	which_style = "/client/stylesheets/Style10.css";

} else if (StyleSheet == "Style12") {
	which_style = "/client/stylesheets/Style12.css";

} else if (StyleSheet == "Style14") {
	which_style = "/client/stylesheets/Style14.css";

} else if (StyleSheet == "Style16") {
	which_style = "/client/stylesheets/Style16.css";

} else if (StyleSheet == "Style48") {
	which_style = "/client/stylesheets/Style48.css";

} else if (StyleSheet == "Style72") {
	which_style = "/client/stylesheets/Style72.css";

} else {
	which_style = "/client/stylesheets/Style.css";
}

document.write("<link rel='stylesheet' href='" + which_style + "' type='text/css'>");



function ck_getCookie (vName) {
	arg = vName + '=';
	alen = arg.length;
	clen = document.cookie.length;
	i = 0;
	
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return _ckGetCookieVal (j);
		i = document.cookie.indexOf(' ', i) + 1;
		if (i == 0) break;
	}
	
	return '';
}
function ck_setCookie (vName, vValue, dExpires, vPath, vDomain, bSecure) {
	document.cookie = vName + '=' + escape (vValue) +
	((dExpires) ? '; expires=' + dExpires.toGMTString() : '') +
	((vPath) ? '; path=' + vPath : '') +
	((vDomain) ? '; domain=' + vDomain : '') +
	((bSecure) ? '; secure' : '');
}
function ck_delCookie (vName, vPath, vDomain) {
	if (ck_getCookie(vName)) {
		document.cookie = vName + '=' +
		((vPath) ? '; path=' + vPath : '') +
		((vDomain) ? '; domain=' + vDomain : '') +
		'; expires=Thu, 01-Jan-70 00:00:01 GMT';
	}
}
function _ckGetCookieVal (offset) {
	var sEnd = document.cookie.indexOf (';', offset);
	if (sEnd == -1) sEnd = document.cookie.length;
	
	return unescape(document.cookie.substring(offset, sEnd));
}