/* Common funcions */

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 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 IsNumeric(sText)
{
    var ValidChars = "0123456789 ";
    var IsNumber=true;
    var Char;
 
    for (i = 0; i < sText.length && IsNumber == true; i++) { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1){
            IsNumber = false;
            return IsNumber;
        }
    }
    return IsNumber;
}

function IsAlphanumeric(sText)
{
    var InvalidChars = "_:;?=+(){}[]/\|`!@#$%^*~<>";
    var IsAlphanumeric = true;
    var Char;

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

function IsAlphabetic(sText)
{
    var InvalidChars = "0123456789-._:;,?=+(){}[]/\|!@#$%^&*~<>";
    var IsAlpha = true;
    var Char;

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

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=yes, status=no, toolbar=no, resizable=yes";

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

function setFormElement(theForm, elementName, elementValue) {
  theForm.elements[elementName].value = elementValue;
}

function clearForm(theForm){
  for (i=0;i<theForm.elements.length;i++){
    theForm.elements[i].value = "";
  }
}

function formSubmit(theForm) {
  theForm.submit();
}

function userLogout(msg) {
  if (confirm(msg)) {
    location.href = 'index.php?selection=logout';
  }
}

function setCountryCodes(theForm){
    
    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 == "China"){
		    theForm.country_code.value = "86";
		}
				
		if(theForm.country.options[theForm.country.selectedIndex].value == "Brunei"){
		    theForm.country_code.value = "673";
		}
		
		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 == "Denmark"){
		    theForm.country_code.value = "45";
		}
		
		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 == "Hungary"){
		    theForm.country_code.value = "36";
		}
		if(theForm.country.options[theForm.country.selectedIndex].value == "Hong Kong"){
		    theForm.country_code.value = "852";
		}
		
		if(theForm.country.options[theForm.country.selectedIndex].value == "Italy"){
		    theForm.country_code.value = "39";
		}
		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 == "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 == "Poland"){
		    theForm.country_code.value = "48";
		}
		if(theForm.country.options[theForm.country.selectedIndex].value == "Philippines"){
		    theForm.country_code.value = "63";
		}
		if(theForm.country.options[theForm.country.selectedIndex].value == "Pakistan"){
		    theForm.country_code.value = "92";
		}
		
		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 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 digit */
		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 toggleDir(theForm){
    if(theForm.sortDir.value == "asc"){
        theForm.sortDir.value = "desc";
    } else {
        theForm.sortDir.value = "asc";
    }
}

