/**
 * project: legrandwedding.com
 * filename: common.js
 *
 * @author jeffrey tan (jeffrey@amcasia.com)
 * @version 1.0 on 2009
 * @copyright the audience motivation company asia pte ltd. 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 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 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";
}

/* function to position element in the center of screen */
function relocation(theElement){
 	var startwidth = 4; var startheight = 3;
 	//var startwidth = 16; var startheight = 9;
	//Define image ratio
	var ratio = startheight/startwidth;
	var theHandler = document.getElementById(theElement);
	//Gather browser and current image size
	var imagewidth = $(theHandler).width();
	var imageheight = $(theHandler).height();
	//var browserwidth = $(window).width();
	//var browserheight = $(window).height();
	var browserwidth = $(theHandler).width();		//$(window).width(); 
  var browserheight = $(theHandler).height(); 	//$(window).height();
	var offset;
	//Resize image to proper ratio
	//alert(imagewidth); alert(browserwidth);
	if((browserheight/browserwidth) > ratio){
		$(theHandler).height(browserheight);
		$(theHandler).width(browserheight/ratio);
	} else {
	  $(theHandler).width(browserwidth);
	  $(theHandler).height(browserwidth*ratio);
	}
	$(theHandler).css('left', (browserwidth - $(theHandler).width())/2);
	$(theHandler).css('top', (browserheight - $(theHandler).height())/2);
	
}

function popupwin(url) {
	var features;
	var win = 'popup';
	var ratio = 3/4;
  var popUpWidth = 1024;
  var leftValue = 0;
  var topValue = 0;
  var screenWidth = screen.width;
  var screenHeight = screen.height;
  var width = 0;
  var height = 0;
	/* popup smaller */
	if(screenWidth > popUpWidth){
		width = popUpWidth;
		height = popUpWidth*ratio;
		leftValue = (screenWidth/2) - (width/2);
  	topValue = (screenHeight/2) - (height/2);
  	topValue = ((screenHeight - topValue)>=(height+topValue)) ? 0 : topValue;
	} else {
		width = screen.width - 12;
		height = screen.availHeight - 30;
	}  
  features = "width=" + width + ", "
  	+ "height=" + height + ", "
  	+ "left=" + leftValue + ", "
  	+ "top=" + topValue + ", "
    + "directories=no, location=no, menubar=no, scrollbars=yes, 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 popup a centralised window with varying width and height */
/*
function popupwin(url) {
	var features;
	var win = 'popup';
  
  var ratio = 3/4;
  var maxScreenWidth = 1024;//1280;
  var leftValue = 0;
  var topValue = 0;
  var screenWidth = screen.width;
  var screenHeight = screen.height;
  alert(screen.height);
  var width = screen.availWidth - 12; 		//$(window).width(); 
  var height = screen.availHeight - 30; 	//$(window).height();
  if(screenWidth > maxScreenWidth){
		width = maxScreenWidth;
		height = maxScreenWidth*ratio;
	}
  leftValue = (screen.width)/2 - (width/2);
  topValue = (screen.availHeight)/2 - (height/2);
  alert(width); alert(height);
  alert (topValue);
  //alert($(window).width()); //alert($(window).height());
  //alert(screen.availHeight);
  //alert($(window).width()); alert(screen.availWidth);
  //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=no";

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