//add Book mark in FireFox and IE
function addBookmark(url,title) {
  if (window.sidebar)
  {
    window.sidebar.addPanel(title, url,"");
  }
  else if( document.all )
  {
    window.external.AddFavorite( url, title);
  }
  else
  {
    return true;
  }
}
function emailToFriend()
{
		window.open('/email-page-friends.php?subject='+document.title+'&link= '+window.location.href,'emailtofriend','width=800,height=660,left=10,top=0,scrollbars=yes,menubar=yes,status=yes,resizable=yes');	
}
function refreshImages()
	{
		// Make sure we are using a new rand code.
		var new_url = new String("show-visualcode.php?");
		// Quick and dirty way of converting decimal to hex
		// Quick and dirty way of converting decimal to hex
		var hexstr = "0123456789abcdef";
		for(var i=0; i < 32; i++)
			new_url = new_url + hexstr.substr(Math.floor(Math.random() * 16), 1);
		document.getElementById("verificiation_image").src = new_url;
}
	
function setupPanels()
{
	hideControl("errmessage");

		
}

//******************************************************************************************
//
// A CGI program uses the following syntax to add cookie information to the HTTP header:
// 
// Set-Cookie:   name=value   
// [;EXPIRES=dateValue]   
// [;DOMAIN=domainName]   
// [;PATH=pathName]   
// [;SECURE]
//
// This function sets a client-side cookie as above.  Only first 2 parameters are required
// Rest of the parameters are optional. If no szExpires value is set, cookie is a session cookie.
//
// Prototype : setCookie(szName, szValue [,szExpires] [,szPath] [,szDomain] [,bSecure])
//******************************************************************************************


function setCookie(szName, szValue, szExpires, szPath, szDomain, bSecure)
{
 	var szCookieText = 	   escape(szName) + '=' + escape(szValue);
	szCookieText +=	 	   (szExpires ? '; EXPIRES=' + szExpires.toGMTString() : '');
	szCookieText += 	   (szPath ? '; PATH=' + szPath : '');
	szCookieText += 	   (szDomain ? '; DOMAIN=' + szDomain : '');
	szCookieText += 	   (bSecure ? '; SECURE' : '');
	
	document.cookie = szCookieText;
}

//******************************************************************************************
// This functions reads & returns the cookie value of the specified cookie (by cookie name) 
//
// Prototype : getCookie(szName)
//******************************************************************************************

function getCookie1(szName)
{
 	var szValue =	  null;
	if(document.cookie)	   //only if exists
	{
       	var arr = 		  document.cookie.split((escape(szName) + '=')); 
       	if(2 <= arr.length)
       	{
           	var arr2 = 	   arr[1].split(';');
       		szValue  = 	   unescape(arr2[0]);
       	}
	}
	return szValue;
}


function getCookie(name) {

var cookiename = name + "=";

var ca = document.cookie.split(';');

for(var i=0;i < ca.length;i++) {

var c = ca[i];

while (c.charAt(0)==' ') c = c.substring(1,c.length);

if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);

}

return null;
}

//******************************************************************************************
// To delete a cookie, pass name of the cookie to be deleted
//
// Prototype : deleteCookie(szName)
//******************************************************************************************

function deleteCookie(szName)
{
 	var tmp = 	  			 	 getCookie(szName);
	if(tmp) 
	{ setCookie(szName,tmp,(new Date(1))); }
}

	
//validate_increase
//input check form name
//Return: true  -  right
//        false -  error 
function validate_check(form,activePanelId)
{
var errorMsg="";
switch (activePanelId) {
   case "panel1":
   	return true;
   case "panel2":
   //check panel1
   
   var errorMsg="Date format (YYYY/MM) Error!."
   var number_of_elements = frm.elements.length;
   for(i=0; i<number_of_elements;i++){
	var checkInputVar = frm.elements[i];
	if(checkInputVar.name.substring(0,8)=="listdate"){
		if (isNotNull(trim(checkInputVar.value))==true){
		if (isDateYYYYMM(trim(checkInputVar.value),errorMsg)==false){
	    //errorMsg="Input Error!   1.How many children are covered under the current Order?  cannot be empty.";
		
		checkInputVar.focus();
    	return false;
		}
	}
	}
	
	
	}
	
    	
   
	return true;
	break;

	case "panel3":
     	
   //check panel2
     	return true;
 		break;
   case "panel4":
   //check panel3
  	return true;
	break;
   case "panel5":
   //check panel4
     	return true;
 default:
  	return true;
  } 

return true;

}

// decrease Panel validate check

// Before submit check 

function validateBlogRequestInput(form)
{
    	
	//   subject:	contact_name	Text	No
    var checkInputVar=form.subject;

   	if (isNotNull(trim(checkInputVar.value))==false){
	//errorMsg="Input Error!   1.How many children are covered under the current Order?  cannot be empty.";
	errorMsg="Input Error! Please enter Subject."
	alert( errorMsg );
	checkInputVar.focus();
    return false;
	}

	 var checkInputVar=form.question_Message;

   	if (isNotNull(trim(checkInputVar.value))==false){
	//errorMsg="Input Error!   1.How many children are covered under the current Order?  cannot be empty.";
	errorMsg="Input Error! Please enter message."
	alert( errorMsg );
	checkInputVar.focus();
    return false;
	}
	
	
	// check visual code 
   var checkInputVar=form.vcode;
   if (isNotNull(trim(checkInputVar.value))==false){
	errorMsg="Input Error! Please enter Visual verification code. <br> Type the letters shown in the box"
	alert( errorMsg );
	checkInputVar.focus();
    return false;
   }


   var visual_verification_code = getCookie("visual_verification_code");
   

   if (isNotNull(trim(visual_verification_code))==false){
		errorMsg="system errorInput Error! Please enter Your Email."
		alert( errorMsg );
		checkInputVar.focus();
    	return false;
	}
	else {
		if (trim(visual_verification_code)!=trim(checkInputVar.value).toUpperCase()){
			errorMsg="Input Error! Please enter Right Visual verification code. <br> Type the letters shown in the box"
			alert( errorMsg );
			checkInputVar.focus();
    		return false;
			
		}
	}
	
	
   
	return true;

 }
  
  
  /// 

function clearErrormsg()
{
	  // should be add in IE brower  
      hideControl("errmessage");
	  document.getElementById('errmessage').innerHTML = '';
	  
}


function isNotNull(s)
{
	s=trim(s);
	
	if ((s==null)||(s=="") || (s.lengh==0)){
		return false
	}
	return true;
	
}


 
function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
 }

  

//removes leading and trailing spaces in the string passed in.

function trim( string_to_trim )
{ 
  while (string_to_trim.charAt(0) == " ")
  { 
    // remove leading spaces 
    string_to_trim = string_to_trim.substring(1); 
  } 
  
  while (string_to_trim.charAt(string_to_trim.length - 1) == " ")
  { 
    // remove trailing spaces 
    string_to_trim = string_to_trim.substring(0,string_to_trim.length - 1); 
  } 
  
  return string_to_trim; 
}


//fully replace the alert function

window.alert = function(msg) {
		// should be add in IE brower if hideControl used  
		msg="<p>"+msg+"</p>";
   showControl("errmessage");
   document.getElementById('errmessage').innerHTML = msg;
   /* window.setTimeout(function() {
       document.getElementById('errmessage').innerHTML = '';
   }, 2000); */
   
}

// Show error message 
function showMessage(msg) {
	msg="<p>"+msg+"</p>";
	// should be add in IE brower if hideControl used  
   showControl("errmessage");
   document.getElementById('errmessage').innerHTML = msg;
  }
  
  
  //removes leading and trailing spaces in the string passed in.
function trim( string_to_trim )
{ 
  while (string_to_trim.charAt(0) == " ")
  { 
    // remove leading spaces 
    string_to_trim = string_to_trim.substring(1); 
  } 
  
  while (string_to_trim.charAt(string_to_trim.length - 1) == " ")
  { 
    // remove trailing spaces 
    string_to_trim = string_to_trim.substring(0,string_to_trim.length - 1); 
  } 
  
  return string_to_trim; 
} 

//show div passed in
function showControl(controlId)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(controlId).style.visibility = "visible";
		document.getElementById(controlId).style.display = "block"; 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[controlId].style.visibility = "visible";
		document.getElementById(controlId).style.display = "block"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[controlId].visibility = "visible";
		document.getElementById(controlId).style.display = "block"; 
	}
}

//hide div passed in
function hideControl(controlId)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(controlId).style.visibility = "hidden";
		document.getElementById(controlId).style.display = "none"; 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[controlId].style.visibility = "hidden";
		document.getElementById(controlId).style.display = "none"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[controlId].visibility = "hidden";
		document.getElementById(controlId).style.display = "none"; 
	}
}


/**
 * DHTML date validation script. 
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;


function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,errMsg){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert(errMsg + "The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert(errMsg+"Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert(errMsg+"Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert(errMsg+"Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert(errMsg+ "Please enter a valid date")
		return false
	}
return true
}

function isDateYYYYMM(dtStr,errMsg){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)

	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1)

	strYr=strYear
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	
	year=parseInt(strYr)
	if (pos1==-1){
		alert(errMsg + "The date format should be : YYYY/MM")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert(errMsg+"Please enter a valid month")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert(errMsg+"Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos1+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert(errMsg+ "Please enter a valid date")
		return false
	}
return true
}



// 
function isValidNumeric(s){
	  if(s.match(/(^-?\d\d*[\.|,]\d*$)|(^-?\d\d*$)|(^-?[\.|,]\d\d*$)/)==null) return false;
	  return true;
}
  //
function isValidAplha(s){
	  if(s.match(/(^[_\-a-z]*\s*[_\-a-z]*$)/gi)==null) return false;
	  return true;
}
 //
 function isValidPhone(s){
	  if(s.match(/(^\(*\d*\)*-*\d*-*\d*$)/)==null) return false;
	  return true;
}

function isInteger(s)
{   var i;
   
  
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


  
  
// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
  
function isIntegerInRange (s, a, b)
   {   if (isEmpty(s))
         if (isIntegerInRange.arguments.length == 1) return false;
         else return (isIntegerInRange.arguments[1] == true);

      // Catch non-integer strings to avoid creating a NaN below,
      // which isn't available on JavaScript 1.0 for Windows.
      if (!isInteger(s, false)) return false;

      // Now, explicitly change the type to integer via parseInt
      // so that the comparison code below will work both on
      // JavaScript 1.2 (which typechecks in equality comparisons)
      // and JavaScript 1.1 and before (which doesn't).
      var num = parseInt (s);
      return ((num >= a) && (num <= b));
   }

function isIntegerGreaterEqual (s, a)
   {   if (isEmpty(s))
         if (isIntegerInRange.arguments.length == 1) return false;
         else return (isIntegerInRange.arguments[1] == true);

      // Catch non-integer strings to avoid creating a NaN below,
      // which isn't available on JavaScript 1.0 for Windows.
      //if (!isInteger(s, false)) return false;

      // Now, explicitly change the type to integer via parseInt
      // so that the comparison code below will work both on
      // JavaScript 1.2 (which typechecks in equality comparisons)
      // and JavaScript 1.1 and before (which doesn't).
      var num = parseInt (s);
      return ((num >= a));
   }
//


function checkEnter(e){

//e is event object passed from function invocation
var characterCode 
//literal character code will be stored in this variable

if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}

if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
//document.forms[0].submit() //submit the form
return false
}
else{
return true
}

}

function isValidEmail(s){

if((s.match(/(^[A-Za-z/-9!#-'*+\-/=?_`{-~^]([A-Za-z.-9!#-'*+\-/=?_`{-~^]*[A-Za-z/-9!#-'*+\-/=?_`{-~^])?@([A-Za-z0-9]+\.)+[A-Za-z0-9]+\.?$)/i)==null)) return false;
	  
	  return true;
} 
