//author     : anuradha chamarti
//created on : 07-11-2006
//modified on: 08-11-2006
//modified by: Rakesh

// User Name
var userName = new Array('\"','~','`','!','#','$','%','^','&','*','(',')','+','=','{','[','}',']',':',';',',','<','>','?','/','|','\\','\'',' ');
// Password
var pwdChars = new Array('\"','\'','~','`','(',')','{','[','}',']',':',';',',','<','>','.','?','/','|','\\',' ','=','+');
var current_menu = "home_page";

//funtions to change the color of a specific row to yellow.
function highlightRow(obj){
	var td=obj.parentNode;
	td = td.parentNode;
	td.style.background='#ffffcc';
}

function highlightele(obj){
	var td=obj.parentNode;
	td = td.parentNode;
	td=td.parentNode;
    td=td.parentNode;
	td=td.parentNode;
    td=td.parentNode;
	td.style.background='#ffffcc';
}

//functions to remove the highlighted color of the row to white.
function removeHighlight(obj){
	var td=obj.parentNode;
	td = td.parentNode;
	td.style.background='#ffffff';
}

function removeHighlightTele(obj){
	var td=obj.parentNode;
	td = td.parentNode;
	td=td.parentNode;
    td=td.parentNode;
    td=td.parentNode;
    td=td.parentNode;
	td.style.background='#ffffff';
}

// Removes leading whitespaces
function LTrim( value ) {
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim( value ) {
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
		return LTrim(RTrim(value));
}

// returns true if the string is a valid email
function isEmail(obj, mandatory){
	if(!obj || !obj.value || obj.value.length === 0){
		if(mandatory){
			return false;
		}else{
			return true;
		}
	}
//	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return re.test(obj.value);
}

//returns true if the string is valid US Zipcode or Canada zipcode
function validZipcode(obj, mandatory){
	if(!obj || !obj.value || obj.value.length === 0){
		if(mandatory){
			return false;
		}else{
			return true;
		}
	}
	var re=/^(\d{5}(( |-)\d{4})?)|([A-Za-z]\d[A-Za-z]( |-)\d[A-Za-z]\d)$/i;
	return re.test(obj.value);
}


//returns true if the string is valid US Zipcode or Canada zipcode used in registration.jsp and modifyregistration.jsp
function validPostalcode(obj, mandatory, cvalue){
	if(!obj || !obj.value || obj.value.length === 0){
		if(mandatory){
			return false;
		}else{
			return true;
		}
	}
	
	var reUS=/^(\d{5}(( |-)\d{4})?)$/i;
	var reCA=/^([A-Za-z]\d[A-Za-z]( |-)\d[A-Za-z]\d)$/i;
	if(cvalue==1){
		return reCA.test(obj.value);
	}
	if(cvalue==2){
		return reUS.test(obj.value);
	}
}
/*
* This method will check for the special characters in the element passed as parameter.
* obj : the element to look for special characters : HTML input element
* special : array of special characters : instance of Array object
* mandatory : is the element passed is mandatory or not : boolean
*/
function checkText(obj,special,mandatory){
//	var special;
	if(!obj || obj.length <= 0 || obj.value === ""){
		if(mandatory){
			return false;
		}else{
			return true;
		}
	}

	var text = obj.value;
	for(var j=0;j<special.length;j++){
		if(text.indexOf(special[j]) != -1){
			return false;
		}
	}
	return true;
}

//function to validate the text for special characters.
function textValidate(element){
	var result=true;   
    var val=element.value;
	if((val.indexOf('\"')!= -1)||(val.indexOf('~')!= -1)||(val.indexOf('`')!= -1)||(val.indexOf('!')!= -1)||(val.indexOf('#')!= -1)||(val.indexOf('$')!= -1)||(val.indexOf('%')!= -1)||(val.indexOf('^')!= -1)||(val.indexOf('&')!= -1)||(val.indexOf('*')!= -1)||(val.indexOf('(')!= -1)||(val.indexOf(')')!= -1)||
			 (val.indexOf('_')!= -1)||(val.indexOf('-')!= -1)||(val.indexOf('+')!= -1)||(val.indexOf('=')!= -1)||(val.indexOf('{')!= -1)||(val.indexOf('[')!= -1)||(val.indexOf('}')!= -1)||(val.indexOf(']')!= -1)||(val.indexOf(':')!= -1)||
			 (val.indexOf(';')!= -1)||(val.indexOf(',')!= -1)||(val.indexOf('<')!= -1)||(val.indexOf('>')!= -1)||(val.indexOf('?')!= -1)||(val.indexOf('/')!= -1)||
			 (val.indexOf('|')!= -1)||(val.indexOf('\\')!= -1) ||(val.indexOf('@')!= -1)){
 
		     result=false;
	}
	return result;
}
/**
* This method will check whether a state is selected or not
* state : the element to look for state selection : HTML combo element
*/
function validateState(state,mandatory){
//	var state = document.getElementById('state');
	if((state.selectedIndex == 0 || state.selectedIndex == 14) && mandatory)
		return false;
	return true;
}

/**
*This method will check whether the url is valid or not:it should not be having 
*special characters
*/
function validUrl(obj){
  var strValidChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-+=,?/\&";
  var val=obj.value;
  var strChar;
  for (i = 0; i < val.length; i=i+1){
	strChar = val.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
      	return false;
  }
  return true;
}

/**
 * function to validate first character 
 * @author Hemanth
 */
function validateFirstChar(obj,invalidchars){
	var result=true;
    var firstChar=obj.charAt(0);
	for (i = 0; i < invalidchars.length;i++){
    	if(invalidchars[i].indexOf(firstChar) != -1){
 		     result=false;
 		     break;
 		 }
    
    }
	return result;
}
 
/**
* This method will check whether the element contains only numerics or not
* obj : the element to look for only numerics : HTML input element
* mandatory : is the element passed is mandatory or not : boolean
*/
function isNumeric(obj,mandatory){
//  check for valid numeric strings	
	var val=obj.value;
	if(!obj || !val || val == null || val == "" || val.length === 0){
		if(mandatory)
			return false;
		else
			return true;
	}
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
    for (i = 0; i < val.length && blnResult === true; i=i+1){
    	strChar = val.charAt(i);
    	if (strValidChars.indexOf(strChar) == -1)
        	return false;
    }
	return true;
}

//function to check fields that allow numbers and space.
function validatePhonenumber(obj,mandatory){
		//  check for valid numeric strings	
			var val=obj.value;
			if(!obj || !val || val == null || val == "" || val.length === 0){
				if(mandatory)
					return false;
				else
					return true;
			}
			
			if(val.length == 8 && val.charAt(3) != '-' && val.charAt(3) != ' '){
				return false;
			}
			
			var strValidChars = "0123456789- ";
			var strChar;
			var blnResult = true;
		    for (i = 0; i < val.length && blnResult === true; i=i+1){
		    	strChar = val.charAt(i);
		    	if (strValidChars.indexOf(strChar) == -1)
		        	return false;
		    }
			return true;
		}

//function to check the numbers in a field.
function telLength(element){
    var strValidChars = "0123456789";
    var strChar;
    var count=0;
    var val=element.value;
	if (val.length == 0) return false;
    for (i = 0; i < val.length ; i=i+1)
         {
           strChar = val.charAt(i);
           if (strValidChars.indexOf(strChar) != -1)
           {
             count=count+1;
           }
     }
   return count;
}

//function to display the error messages in the form.
function displayError(error_var){
    var errorData = "<ul class='err'>";
    if(error_var.length !== 0){
     for(var i=0;i<error_var.length;i=i+1){
	    errorData += "<li>"+error_var[i]+"</li>";
      }	   
    }
    errorData += "</ul>";
    error_var = new Array();
    var content_div = document.getElementById("content");
	var html = "";
		html = "<br>";
	var temp_div = document.createElement("div");
	temp_div.innerHTML = errorData;
	temp_div.className = "normalText";
	temp_div.style.width = "100%";
	temp_div.align = "center";
	temp_div.valign = "middle";

	html  += "<table id = \'errormsgs\' align=\'center\'  style=\'background-color:#ffffcc;width:60%;line-height:25px;border:1px solid #cccccc;\'>"
			+"<tr><td align=\'left\' valign=\'top\'><img src=\'images/alrt.gif\' width=\'42px\' height=\'41\'></td><td class=\'errorMsg\'>"+temp_div.innerHTML+"</td></tr></table></div>";
	
	var err_div = document.getElementById("error_div");
	err_div.style.visibility = "visible";
	err_div.innerHTML = "";
	err_div.innerHTML = html;
	if(content_div)
	  content_div.focus();
}
/*
//function to confirm the cancellation process.
function cancel_confirm(){
	var name=confirm("Are you sure you want to cancel your registration? Click Cancel to continue with registration.");
	if (name === true){
		window.location.href = "home.jsp";
	}
}
*/
//function to change the id of the current page to 'current'.
function updateCurrent(pageid){
	var menuTab = document.getElementById("current");
	if(menuTab && current_menu){
		menuTab.id = current_menu;
	}
	menuTab = document.getElementById(pageid);
	if(menuTab){
		current_menu = menuTab.id;
		menuTab.id = "current";
	}
}
	
//validation for the check box for email me at both the email ids.
function enableEmailMe(){
	var secEmail=document.getElementById('umSecondaryEmail');
	var emailme=document.getElementById('umEmailMe');
	var emailval= secEmail.value;
	if(emailval.length !== 0){
	    emailme.disabled=false;
	}
	else{
	  emailme.disabled=true;
	  emailme.checked=false;
	 }
}


function showPreview(page,width,height){
	var xPos = screen.availWidth - width;
	var yPos = screen.availHeight - height;
	xPos = xPos/2;
	yPos = yPos/2;
		
	win_popup = window.open(page,"popup","directories=no,height="+height+",location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,width="+width+",modal=yes,top="+yPos+",left="+xPos);
	win_popup.focus();
}

			
function hideWelcomeMsg(){
	document.getElementById("toplinkMsg").style.display="none";
}

function loadAdvertizement(el) {
	var adifyFrame = document.getElementById(el);
	if (adifyFrame) {
		adifyFrame.src = "page_components/adify/banner120x240.html";
	}
}

function getCookie() {
	var c_name = "username";
	var username = document.getElementById('username');
	var pwd = document.getElementById("password");
	if(username && pwd) {
	//	setTimeout('focusonLoginSubmitButton()',100);
		if (document.cookie.length > 0) {
		   	c_start = document.cookie.indexOf(c_name+"");
	  		if (c_start != -1) {
				c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) {
					c_end = document.cookie.length;
				}
				var  user=  (document.cookie.substring(c_start, c_end));
				if(!username.value){
					 username.value=user;
				}
				pwd.focus();
			}else
				username.focus();
		}else{
			username.focus();
			return null;
		}
	}
}				
