///////////////////////////////////////////////////////////////////
//
// utilCreateWindow(url, window_width, window_height, position, options, wName)
//
// Function to create and position a new window
// Parameters:
//    1. URL - The URL to open in the new window
//    2. Width of window - Width of the new window in pixels
//    3. Height of window - Height of the new window in pixels
//    4. [Optional] Position of window - center, top - default center
//    5  [Optional] Options - status, scrollbars, resizable
//    6. [Optional] Window name
//    Ex: utilCreateWindow("<url>", 300, 250);

// Declare a global variable for the new window object
var newWindow;

function utilCreateWindow(url, window_width, window_height, position, options, wName)
{ 
    position = (position == null) ? "center" : position;
    options = (options == null) ? "" : options + ",";
  
    // Default features:  statusbar, scrollbars, and window can be resized
    window_features = options + "width=" + window_width + "," + "height=" + window_height;
  
	newWindow = window.open(url, (wName == null ? "" : wName), window_features);

    //Get the available width and height of the screen
    x=(screen.availWidth-window_width);
    y=(screen.availHeight-window_height);

    xpos=(x/2);

    ypos = (position == "center") ? (y/2) : 1;
	

    if (xpos > 0 && ypos > 0)
    {
      newWindow.moveTo(xpos, ypos);
    }
}


   
///////////////////////////////////////////////////////////////////
// 
// Function to check if an e-mail address is valid
// Parameters:
//    1. Text element object - e-mail address 
//       As per RFC 2822 (Internet Message Format) , local-part of an e-mail address (before @) 
//		 can contain the following:
//		 Uppercase and lowercase letters (case sensitive)
//		 The digits 0 through 9
//		 The characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
//		 The character . provided that it is not the first or last character in the local-part.
// Returns true if the string is a valid e-mail address, false otherwise

function isValidEmailAddress(emailAddress){
	var lstr = emailAddress.length;
    var iCharAtPosition = emailAddress.indexOf("@");
    var lastDotPosition = emailAddress.lastIndexOf(".");
    
    var regex=/^[a-zA-Z\.]+$/;
    var startDomainRegex = /^[a-zA-Z\d]+$/;
    var startDomain = emailAddress.substring(iCharAtPosition + 1,emailAddress.indexOf(".",iCharAtPosition));
    var endDomain = emailAddress.substring(emailAddress.indexOf(".",iCharAtPosition) + 1,lstr);
    
    if(!startDomainRegex.test(startDomain)){
    	return false;
    }
    
    if(!regex.test(endDomain)){
    	return false;
    }
    
    if(lastDotPosition == -1){
    	return false;
    }
    if(lstr-lastDotPosition > 4 || lstr-lastDotPosition < 3){
    	return false;
    }
    
    if(lastDotPosition < iCharAtPosition){
    	return false;
    }
    
    if (iCharAtPosition == -1){
    	return false;
	}
	else if ((iCharAtPosition == 0) || (iCharAtPosition == (lstr - 1))){
    	return false;
	}
	else if (emailAddress.indexOf("@", iCharAtPosition + 1) > -1){
    	return false;
	}
	else{
		for (i = 0; i < lstr; i++){
           	var c = emailAddress.charAt(i);
           	if (c == '.'){
           		if ((i == 0) || (i == (lstr - 1))){
               		return false;
				}
				else if (i == (iCharAtPosition-1)){
               		return false;
				}
              	else if (i == (iCharAtPosition+1)){
                	return false;
				}
			}
			else if ( c > '~' || c < '!' || c == '(' || c == ')' || 
                     c == '<' || c == '>' || c == '[' || c == ']' || 
                     c == ',' || c == ';' || c == ':' || c == '\\' ||
                     c == '"'){
            	return false;
			}
		}
	}
    return true; 
 }
///////////////////////////////////////////////////////////////////
// 
// Function to check if goldcard number is valid
// Parameters:
//    1. Text element object - gcnumber 
//      Valid format is mc followed by a space(optional)
//		and 6 digits 
// Returns true if the string is a valid goldcard number, false otherwise
function isValidGoldcardNumber(gcnumber){
   return (isValidGoldCardNumber2009(gcnumber));
}

function isValidGoldCardNumber2008(gcnumber){
      var regexpd =/^PD?\d{6}$/i;
      
  if(!regexpd.test(gcnumber)){
      return false;
    }
    
  if((gcnumber.substring(2,8) >= 000001 && gcnumber.substring(2,8) <= 517100)) {
     return true;
     } 
     
   return false;
}

function isValidGoldCardNumber2009(gcnumber){
       var regexpd =/^GA\s?\d{6}$/i;
      
  if(!regexpd.test(gcnumber)){
      return false;
    }
    
  if((gcnumber.substring(2,8) >= 000001 && gcnumber.substring(2,8) <= 517100)) {
     return true;
     } 
     
   return false;
}

///////////////////////////////////////////////////////////////////
// 
// Function to check if phone number is valid
// Parameters:
//    1. Text element object - areacode
// 			3 digit number
//	  2. Text element Objetc - prefix
//			3 digit number
//	  3. Text element Object - num
//			4 digit number
//
// Returns true if the string is a valid phone number, false otherwise
function isValidPhoneNumber(areaCode,prefix,num){
	if( (areaCode.value.length > 0) || (prefix.value.length > 0) || (num.value.length > 0) ){
		var checkOne = /^\d{3}$/;
		var checkTwo = /^\d{4}$/;
		if( !checkOne.test(areaCode.value) || !checkOne.test(prefix.value) || !checkTwo.test(num.value) ){
			return false;
		}
	}
	return true;
}

//Fuction to trim the given String
String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
 };

//Function to refresh the page on clicking back
function reloadPage(){
   	var e=document.all.refreshed;
	if(e.value=="no")e.value="yes";
	else{e.value="no";location.reload();}
}

//Function to refresh the register page on clicking back
function reloadRegister(){
   	var e=document.all.refreshed;
	if(e.value=="no")e.value="yes";
	else{e.value="no";
	window.location = "register.do";
	}
}

//Function to refresh the change password page on clicking back
function reloadChangePassword(){
   	var e=document.all.refreshed;
	if(e.value=="no")e.value="yes";
	else{e.value="no";
	window.location = "login.do?action=changepassword";
	}
}	

//Function to refresh the forget password page on clicking back
function reloadForgetPassword(){
   	var e=document.all.refreshed;
	if(e.value=="no")e.value="yes";
	else{e.value="no";
	window.location = "login.do?action=forgetpassword";
	}
}	

//Function to refresh the survey page on clicking back
function reloadSurvey(){
   	var e=document.all.refreshed;
	if(e.value=="no")e.value="yes";
	else{e.value="no";
	window.location = "survey.do?action=takeSurvey";
	}
}	

// Simplied function to create a popup window
function createWindow(url, w, h){
	var load = window.open(url,'','scrollbars=no,menubar=no,height=' + h + ',width=' + w + ',resizable=no,toolbar=no,location=no,status=no');

}		
