/*
This Javascript file aims to group together miscellaneous scripts so that they need to be loaded only once
It should only contain scripts that are used in several pages (not just one or 2)
You are free to add some functions to it if they are relevant. Watch out variables names duplicates
*/


function launchCenter(url, name, width, height, features, setFocus) {
  var str = "height=" + height + ",innerHeight=" + height;
  str += ",width=" + width + ",innerWidth=" + width +" , "+features;
  if (window.screen) {
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;

    var xc = (aw - width) / 2;
    var yc = (ah - height) / 2;

    str += ",left=" + xc + ",screenX=" + xc;
    str += ",top=" + yc + ",screenY=" + yc;
  }
  var theWin = window.open(url, name, str);
  theWin.focus();
  return theWin;
}

function launchPosition(url, name, width, height, top, left, features) {
  var str = "height=" + height + ",innerHeight=" + height;
  str += ",width=" + width + ",innerWidth=" + width;
  str += ",left=" + left + ",screenX=" + left;
  str += ",top=" + top + ",screenY=" + top;

if(features != "") str += ", "+features;
else str += ",status=1, toolbar=1, location=1, menubar=1, directories=1, resizable=1, scrollbars=1";

  var theWin = window.open(url, name, str);
//  theWin.resizeTo(width+30,height+100);
  theWin.focus();
  return theWin;
}

function printArt() {
		if (document.all){
//		alert(document.all['text'].innerHTML);
		var win1 = window.open('', 'Example1','top=30, left=30, width=750, height=600, resizable=yes, scrollbars=yes');
		win1.document.open();
		win1.document.write('<head><title>'+ document.all['titre'].innerText +'</title><link rel="stylesheet" href="/objects/css/print.css" type="text/css"></head><body onLoad="window.print()">');
		win1.document.write('<table width="100%" cellspacing="0" cellpadding="7" align="center"><tr><td><img src="/objects/img/rsd150.gif" width="159" height="58" id="logo"></td></tr>');
		win1.document.write('<tr><td><h1>'+ document.all['titre'].innerText +'</h1></td></tr><tr><td>'+ document.all['text'].innerHTML +'</td></tr>')
		win1.document.write('<tr><td><div id="address"><span class="compName">RSD S.A.</span><br>41, rue Marziano, CP 1750<br>1211 Geneva 26, Switzerland<br>Tel. +41 22 307 81 00<br>Fax +41 22 307 81 01<br>Email: ventes@rsd.com</div></td></tr>');
//		win1.document.write('<div align="center"><a href="javascript:window.close();">Close</a></div><body></html>')
		win1.document.write('</table></body></html>')
		win1.document.close();
		}
		else {
		window.print();
		}
}


//	MISC GENERIC DW LIKE FUNCTIONS
function displayStatusMsg(msgStr) { 
  status=msgStr;
  document.returnValue = true;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/*
COOKIES MANAGEMENT : Language, and others if needed
*/
function setLang(langParam, URL){
//	URL = "/";
	//Manage Cookie expiry date
	var nextyear=new Date();
	var lastyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear()+1);
	lastyear.setFullYear(lastyear.getFullYear()-1);
	// Set cookie
	document.cookie = "rsdSiteLang=" + langParam + "; expires=" + nextyear.toGMTString(); + "; path=/";
	document.location.replace(URL);
}
// function below not used: not needed as changing language using select, you specify a precise URL (fr or en).
function initLangSelect(){
langCookieValue = getCookie('rsdSiteLang');	
//alert(langCookieValue);
if(langCookieValue == '') langCookieValue = 'en';
document.forms[0].langSwitch.value = langCookieValue;
}

// Generic function to retrieve a cookie's value:
function getCookie (cookieName) {
    var _allCookies = self.document.cookie;
    var _pos = _allCookies.indexOf(cookieName + "=");
    // Set the variable _value to default value that you
    // want to be returned if the cookie with the given
    // name isn't found
    var _value = "";
    if (_pos !=-1) {
        var _start = _pos + cookieName.length+1;
        var _end = _allCookies.indexOf(";", _start);
        if (_end == -1) _end = _allCookies.length;
        _value = _allCookies.substring(_start, _end);
    }
	return _value;
}

// PUTS URL ARGUMENTS IN AN ARRAY
function getQuery() {
    var retObj=new Object();
    var str   = unescape( (self.location.search).substring(1) );
    var pairs = str.split('&');
    var pair;
    for (x in pairs) {
        pair=pairs[x].split('=');
        if (pair.length == 2)
            retObj[pair[0]] = pair[1];
        }
    return retObj;
    }

// Generic DOM objects reference retrieval and visibility change Functions
// Locate an object by ID and return it
function getById(divId){
var theobj;
	if(document.getElementById){
		theObj = document.getElementById(divId);
	}
	else if(document.all){
		theObj = document.all.divId;
	}
return theObj;
}


// Show or Hide a layer - Parameters : object ID, show/hide
function showOrHideLayer(divId, action){
	var theobj;
	theObj = getById(divId);
	if(action == 'show') theObj.style.visibility = 'visible';
	else theObj.style.visibility = 'hidden';
}

// Toggle object Display value (none, block)
function toggleLayer(divId, action){
	var theobj;
	theObj = getById(divId);
	if(action == 'show') theObj.style.display = 'block';
	else theObj.style.display = 'none';
}

// Function to get show/hide status on object
function getToggleStatus(divId){
	var theobj;
	theObj = getById(divId);
	if(theObj.style.display == 'block') return "hide";
	else return "show";
}

// Function to toggle a div according on its status
function toggleAuto(divId){
	toggleLayer(divId,getToggleStatus(divId));
}


/* Generic function to Swap image(s).
	Param 1: img object reference (this)
	Param 2: img src to swap to.
*/
function swapImgTo(imgObj,newSrc){
	imgObj.src=newSrc;
}


// Generic function to switch Tabs Content Display & Tabs Class Name
// Syntax for div IDs in HTML:
// 		Tabs: tab1, tab2, ...
//		Content tabs: tab-content-1, tab-content-2, ...
function switchTabs(tabNbr, NbrOfTabs){
	for(var i=1;i<NbrOfTabs+1;i++){
		var tabObj = getById("tab"+i);
		var tabContentObj = getById("tab-content-"+i);
		if(tabNbr == i){
			tabObj.className = "switch-tab-on";
			tabContentObj.style.display = "block";
		}
		else{
			tabObj.className = "switch-tab-off";
			tabContentObj.style.display = "none";
		}
	}
}


// Generic PDF access function: open PDF access form
/*function loadDocAccessForm(docRef, docName, docType){
	var winName = "docAccess"+Math.floor(Math.random()*150461258);
	// First, check doc access form session cookie
	var formPath = "/marketing/DocumentAccessForm.html";
	if(getCookie("docAccessFormSession") != "") formPath = "/marketing/DocumentAccessForm_.html";
	var docRef = escape(docRef);
	var docName = escape(docName);
	var formUrl = formPath+'?docRef='+docRef+'&docName='+docName+'&docType='+docType;
	launchCenter(formUrl,winName,800,600,'status=yes, scrollbars=auto, resizable=yes');
} */
// Same as above, but with additional phone number field + Mngt
function loadDocAccessForm(docRef, docName, docType){
	var winName = "docAccess"+Math.floor(Math.random()*150461258);
	var docRef = escape(docRef);
	var docName = escape(docName);
	var formPath = "/marketing/DocumentAccessForm2.html";
	if(getCookie("docAccessFormSession2") != "") formPath = "/marketing/DocumentAccessForm2_.html";
	var formUrl = formPath+'?docRef='+docRef+'&docName='+docName+'&docType='+docType;
	launchCenter(formUrl,winName,800,600,'status=yes, scrollbars=auto, resizable=yes');
}
// Same but for FRENCH site
/*function loadDocAccessForm_fr(docRef, docName, docType){
	var winName = "docAccess"+Math.floor(Math.random()*150461258);
	// First, check doc access form session cookie
	var formPath = "/marketing/DocumentAccessForm_fr.html";
	if(getCookie("docAccessFormSession") != "") formPath = "/marketing/DocumentAccessForm_fr_.html";
	var docRef = escape(docRef);
	var docName = escape(docName);
	var formUrl = formPath+'?docRef='+docRef+'&docName='+docName+'&docType='+docType;
	launchCenter(formUrl,winName,800,600,'status=yes, scrollbars=auto, resizable=yes');
}*/

// Same but for FRENCH site
function loadDocAccessForm_fr(docRef, docName, docType){
	var winName = "docAccess"+Math.floor(Math.random()*150461258);
	// First, check doc access form session cookie
	var formPath = "/marketing/DocumentAccessForm_fr2.html";
	if(getCookie("docAccessFormSession2") != "") formPath = "/marketing/DocumentAccessForm_fr_2.html";
	var docRef = escape(docRef);
	var docName = escape(docName);
	var formUrl = formPath+'?docRef='+docRef+'&docName='+docName+'&docType='+docType;
	launchCenter(formUrl,winName,800,600,'status=yes, scrollbars=auto, resizable=yes');
}

// Generic flash viewer call
function viewFlash(flash_url,title,width,height,lang){
	var page_title = ""+title;
	var close_text = "Close";
	var replay_text = "Replay";
	if(lang == "fr"){
		page_title = ""+title;
		close_text = "Fermer";
		replay_text = "Relire";
	}
	var viewer_url = "/objects/php/flash-viewer.php?title="+escape(page_title)+"&url="+flash_url+"&close_text="+close_text+"&replay_text="+replay_text+"&width="+width+"&height="+height;
	launchCenter(viewer_url,"viewFlash"+lang,width+40,height+80,'status=yes, scrollbars=auto, resizable=yes');	
	
}


/* GENERIC FORM VALIDATION FUNCTIONS */

// Check Email syntax is valid
function checkEmailSyntax(emailObj){
   var testresults
   em=emailObj;
   emv=em.value;
   filter=/^.+@.+\..{2,4}$/
   if (emv != "") {
      if (filter.test(emv)) {
         testresults=true
      } else {
         alert("Please input a valid e-mail address!")
         em.focus();
         em.select();
         testresults=false;
      }
         return (testresults)
   }
}

// Function to check that at least one checkbox is checked
function boxChecked(formObj,nameCode){
// Loop through all document checkboxes, processing only those starting with nameCode value
for(var i=0;i<formObj.elements.length;i++){
	formEl = formObj.elements[i];
	if(formEl.type=="checkbox" && formEl.name.indexOf(nameCode) != -1){
		if(formEl.checked) return true;
		}
	}
return false;
}
// Function to check that at least one radio button is checked
function radioChecked(formObj,nameCode){
// Loop through all document checkboxes, processing only those starting with nameCode value
for(var i=0;i<formObj.elements.length;i++){
	formEl = formObj.elements[i];
	if(formEl.type=="radio" && formEl.name.indexOf(nameCode) != -1){
		if(formEl.checked) return true;
		}
	}
return false;
}

// Check numeric value is entered
function isInteger(val){
for(var i=0;i<val.length;i++){
	if(!isDigit(val.charAt(i))){return false;}
}
return true;
}
function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}

