/**
 * project: clstudio.com.sg
 * filename: common.js
 *
 * @author jeffrey tan (jeffrey_design@yahoo.com)
 * @version 1.0 on aug 2008
 * @copyright jeffrey tan. all rights reserved.
 */

/* functions for mouseover effects for navigation bar */
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; 
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
		x.src=x.oSrc;
}

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];}
  }
}

function MM_findObj(n, d) { //v4.0
	var p,i,x;  if(!d) d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length){
  	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; 
	document.MM_sr=new Array; 
	for(i=0;i<(a.length-2);i+=3)
  	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* function to the value of a string */
function trim(s) { // remove leading spaces and carriage returns
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) { 
		s = s.substring(1,s.length);
	}

  // remove trailing spaces and carriage returns
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
		s = s.substring(0,s.length-1); 
	}

	return s;
}

/* function to if the value of a string is numeric */
function IsNumeric(sText) {
	var ValidChars = "+-0123456789 ";
  var IsNumber = true;
  var Char;

	for (i = 0; i < sText.length && IsNumber; i++) {
  	Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) IsNumber = false;
  }
  return IsNumber;
}

/* function to check if the value of a string is alphanumeric */
function IsAlphanumeric(sText) {
	/* var InvalidChars = "_:;?=+(){}[]/\|`!@#$%^*~<>"; */
	var InvalidChars = "_:;?={}[]/\|`!@#$%^*~<>";
  var IsAlphanumeric = true;
  var Char;

  for (i=0; i<sText.length && IsAlphanumeric; i++) { 
  	Char = sText.charAt(i);
    if (InvalidChars.indexOf(Char) != -1) IsAlphanumeric = false;
  }
  return IsAlphanumeric;
}

/* function to check if the value of a string is alphabetic */
function IsAlphabetic(sText) {
//var InvalidChars = "0123456789-._:;,?=+(){}[]/\|!@#$%^&*~<>";
	var InvalidChars = "0123456789_:;,?=+(){}[]/\|!@#$%^&*~<>";
  var IsAlpha = true;
  var Char;

  for (i=0; i<sText.length && IsAlpha; i++) { 
  	Char = sText.charAt(i);
    if (InvalidChars.indexOf(Char) != -1) IsAlpha = false;
  }
  return IsAlpha;
}

/* function to popup a centralised window with varying width and height */
function popupwin(url, win, w, h) {
	var features, leftValue, topValue;
  var width = w;
  var height = h;

  leftValue = (screen.availWidth)/2 - (width/2);
  topValue = (screen.availHeight)/2 - (height/2);

  features = "width=" + width + ", "
  	+ "height=" + height + ", "
    + "left=" + leftValue + ", "
    + "top=" + topValue + ", "
    + "directories=no, location=no, menubar=no, scrollbars=no, status=no, toolbar=no, resizable=no";

	previewWin = window.open(url, win, features);     // show browser window
	previewWin.focus();    														// bring minimise window back on focus
}

/* function to set value for form element */
function setFormElement(theForm, elementName, elementValue) {
	theForm.elements[elementName].value = elementValue;
}

/* function to clear the value for all form elements */
function clearForm(theForm) {
	for (i=0;i<theForm.elements.length;i++) theForm.elements[i].value = "";
}

/* function to submit a form */
function formSubmit(theForm) {
  theForm.submit();
}

/* function to get user confirmation to logout */
function userLogout(msg) {
  if (confirm(msg)) location.href = 'index.php?selection=logout';
}

/* function to auto polulate country codes based on country selected */
function setCountryCodes(theForm){
	// reset values
  theForm.country_code.value = "";
  theForm.area_code.value = "";

	if(theForm.country.options[theForm.country.selectedIndex].value == "Australia") {
		theForm.country_code.value = "61";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Brunei") {
		theForm.country_code.value = "673";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "China") {
		theForm.country_code.value = "86";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Denmark") {
		theForm.country_code.value = "45";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Finland") {
		theForm.country_code.value = "358";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "France") {
		theForm.country_code.value = "33";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Germany") {
		theForm.country_code.value = "49";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Greece") {
		theForm.country_code.value = "30";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Hong Kong") {
		theForm.country_code.value = "852";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Hungary") {
		theForm.country_code.value = "36";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Iceland"){
		theForm.country_code.value = "354";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "India") {
		theForm.country_code.value = "91";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Indonesia") {
		theForm.country_code.value = "62";
	}	
	if(theForm.country.options[theForm.country.selectedIndex].value == "Italy") {
		theForm.country_code.value = "39";
	}		
	if(theForm.country.options[theForm.country.selectedIndex].value == "Japan") {
		theForm.country_code.value = "81";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Malaysia") {
		theForm.country_code.value = "60";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Norway") {
		theForm.country_code.value = "47";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "New Zealand") {
		theForm.country_code.value = "64";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Pakistan") {
		theForm.country_code.value = "92";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Philippines") {
		theForm.country_code.value = "63";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Poland") {
	  theForm.country_code.value = "48";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Russia") {
		theForm.country_code.value = "7";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Spain") {
		theForm.country_code.value = "34";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Sweden") {
		theForm.country_code.value = "46";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Switzerland") {
		theForm.country_code.value = "41";
	}		
  if(theForm.country.options[theForm.country.selectedIndex].value == "Singapore") {
		theForm.country_code.value = "65";
		theForm.area_code.value = "0";
	}		
	if(theForm.country.options[theForm.country.selectedIndex].value == "South Korea") {
		theForm.country_code.value = "82";
	}		
	if(theForm.country.options[theForm.country.selectedIndex].value == "Taiwan") {
		theForm.country_code.value = "886";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Thailand") {
		theForm.country_code.value = "66";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "The Netherlands") {
		theForm.country_code.value = "31";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Turkey") {
		theForm.country_code.value = "90";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "United States") {
		theForm.country_code.value = "01";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "United Kingdom") {
		theForm.country_code.value = "44";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Ukraine") {
		theForm.country_code.value = "380";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Vatican City") {
		theForm.country_code.value = "39";
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Vietnam") {
		theForm.country_code.value = "84";
	}	
}

/* function to verify a given credit card number */
function IsValidCreditCard(theNumber){
	// reverse the number
	r = "";
	for(i=(theNumber.length - 1); i>=0; i--){
		c = parseInt(theNumber.charAt(i),10);
		r = r + c;
	}

	// double alternate digit
	t = "";
	for(i=0; i<r.length; i++){
		c = parseInt(r.charAt(i),10);
		if(i%2 != 0){
			c *= 2;
		}
		t = t + c;
	}

	// sum digits
	n = 0;
	for(i=0; i<t.length; i++){
		c = parseInt(t.charAt(i), 10);
		n = n + c;
	}

	// analyse result
	if(n!=0 && (n%10 == 0)){
		return true;
	} else {
		return false;
	}	
}

/* function to verify given email address */
function isValidEmail(theEmail){
	var atPos, dotPos
	
	atPos = theEmail.indexOf("@",1)
	if (atPos == -1) return false;	
	dotPos = theEmail.indexOf(".",atPos)
	if (dotPos == -1) return false;
	if (dotPos+2 > theEmail.length) return false
	
	return true;
}

/* function to toggle sorting direction - asc/desc */
function toggleDir(theForm){
	// default is ascending (asc)
	if(theForm.sortDir.value == "asc") theForm.sortDir.value = "desc";
	else theForm.sortDir.value = "asc";
}