 /*  #################        HEADER INFORMATION        #####################
    ------------------------------------------------------------------------
    Purpose of page        : common functions
    Page Name              : function.js
    Version Information    : 1st version
    Output Page            : functional dependant
    Date & Time            : 30th June 2006
    Created By             : thaiguys.com team
    Modified               : 30th June 2006
    ------------------------------------------------------------------------
   #################        HEADER INFORMATION        ######################
  */

function addbookmark(pageurl,pagetitle)
  {
	         
			var agt=navigator.userAgent.toLowerCase();
			title="BoysNextDoor.com=>"+pagetitle;
		    url="http://www.boysnextdoor.com/user/"+pageurl;
		   					
       if (agt.indexOf("firefox") != -1)
		   {
			  //if (window.sidebar)
			   // { 
				     window.sidebar.addPanel(title,url,"");
		       // }
			  
			}
			//if(navigator.vandor=="Apple")
		 
		else if (agt.indexOf("safari") != -1)

			{ 
			      alert(" Press(Apple-D) on your keyboard.");
                
			}
		else if((agt.indexOf("msie") != -1)&&(navigator.platform.indexOf('Mac')!=-1))
			{
				  alert(" Press(Apple-D) on your keyboard.");
			}
			
		 else if((agt.indexOf("msie") != -1)&&(navigator.platform.indexOf('Win')!=-1))
			{
			   //	if( window.external )
			    // { 
					 window.external.AddFavorite(url,title)
	    	    // }
		    }
		 
		 else if (agt.indexOf("netscape") != -1)
			{
				alert(" Press(Ctrl-D) on your keyboard.");
			}
		 else if (agt.indexOf("mozilla/5.0") != -1)
			{
				alert(" Press(Ctrl-D) on your keyboard.");
			}
		else if(agt.indexOf("opera") != -1)
			{
				if(window.opera && window.print)
		        { 
		           return true;
		        }
			}
			
}

/*
//<body onload="
	if( window.sidebar || ( window.ActiveXObject && navigator.platform.indexOf('Mac') + 1 ) ) 
		{
		for( var i = 0, toChange = [document.links,document.forms]; toChange[i]; i++ ) 
		  {
			for( var x = 0; toChange[i][x]; x++ ) 
			  {	
				if( toChange[i][x].target == '_main' ) 
			     {
					toChange[i][x].target = window.sidebar ? '_content' : '';
				 }
			   }
		   }
     	}
//">How to install sidebar panels
*/		
			
/*
function addbookmark(pageurl,pagetitle)	
{
	title="ThaiGuys.com=>"+pagetitle;
		url="http://www.thaiguys.com/user/"+pageurl;
		
		if (window.sidebar)
			{ // Mozilla Firefox Bookmark	
			  window.sidebar.addPanel(title,url,"");
		    }
		else if( window.external )
			{ // IE Favorite
		      window.external.AddFavorite(url,title);
	    	}
		else if(window.opera && window.print)
		    { // Opera Hotlist	
		      return true;
		    }
	}*/
		

function is_empty(obj, str)
{
  if ( (obj.type=="text") || (obj.type=="password") || (obj.type=="textarea") || (obj.type=="file"))
  {
    
     if(obj.value=="")
     {
	   alert('Please enter ' + str +'.');
	   obj.focus();
	   return true;
     }
  }
	
  if (obj.type=="select-one")
  {
	  if (obj.selectedIndex==0)
	  {
		  alert('Please select ' + str+ '.' );
		  obj.focus();
		  obj.select;
		  return true;
	  }
  }
  return false;
}


function isValidEmail(obj)
{
	if(!is_empty(obj, "Email")) {
		if(! echeck(obj.value) )
		{
			alert("Please enter valid Email.");
			obj.focus();
			return false;
		}
	}
	else
	{
		return false;
	}
	return true;
}

function isValidLoginId(obj)
{
	if(!is_empty(obj, "Login Id")) {
		if(! echeck(obj.value) )
		{
			alert("Please enter valid Login Id.");
			obj.focus();
			return false;
		}
	}
	else
	{
		return false;
	}
	return true;
}
/// Email Checking Pass
function echeck(str) {

   var at="@"
   var dot="."
   var lat=str.indexOf(at)
   var lstr=str.length
   var ldot=str.indexOf(dot)

   if (str.indexOf(at)==-1){
       return false
   }

   if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false
   }

   if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
       return false
   }

   if (str.indexOf(at,(lat+1))!=-1){
      return false
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
   }

   if (str.indexOf(dot,(lat+2))==-1){
   return false
   }

   if (str.indexOf(" ")!=-1){
      return false
   }
   return true
}// End Function


//removes the trailing spaces
function trim(pstrString)
{
  var intLoop=0;
  for(intLoop=0; intLoop<pstrString.length; )
  {
      if(pstrString.charAt(intLoop)==" ")
         pstrString=pstrString.substring(intLoop+1, pstrString.length);
      else
         break;
  }

  for(intLoop=pstrString.length-1; intLoop>=0; intLoop=pstrString.length-1)
  {
      if(pstrString.charAt(intLoop)==" ")
         pstrString=pstrString.substring(0,intLoop);
      else
         break;
  }
  return pstrString;
}


//Validation for Phone numbers

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

 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;
}

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
 var s=stripCharsInBag(strPhone,validWorldPhoneChars);
 return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

//function zipcode validations
 function validatezip(zip)
 {
	 var valid = "0123456789-";
	 var hyphencount = 0;
	 var field=zip;
	 
	 if(field.length!=5 && field.length!=10) 
	  {
	   alert("Please Enter your 5 digit or 5 digit+4 zip code.");
	   return false;
	  }
	 for(var i=0; i < field.length; i++)
	 {
	  temp = "" + field.substring(i, i+1);
	   if(temp == "-") hyphencount++;
	   if(valid.indexOf(temp) == "-1") 
		   {
			 alert("Invalid zip code.");
			 return false;
		   }
	  if((hyphencount>1) || ((field.length==10) && ""+field.charAt(5)!="-"))
	   { 
		alert("Invalid Zip code");
		return false;
	   }
	 return true;
	 }
 }

 //function  validate date
function testdate(day,month,year)
{ 
	 var myDayStr = day;
	 var myMonthStr = month;
	 var myYearStr = year;
	 		 
	var myMonth = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

	var myDateStr = myDayStr + ' ' + myMonth[myMonthStr] + ' ' + myYearStr;

	/* Using form values, create a new date object
	using the setFullYear function */
	var myDate = new Date();
	myDate.setFullYear( myYearStr, myMonthStr, myDayStr );
	//alert(myDate.getMonth());
	//alert(myMonthStr);
	if ( myDate.getMonth() != myMonthStr )
	{
		 alert( 'Sorry, "' + myDateStr + '" is NOT a valid date.' );
	}
	else
	{
	  //alert( 'Congratulations! "' + myDateStr + '" IS a valid date.' );
	   return true;
	}
  
}



//perfect email validation
function emailCheck(emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

//alert("Invalid email Id.Please check @ and .'s)");
alert("Invalid Email id!");

return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
//alert("Ths username contains invalid characters.");
alert("Invalid Email id!");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
//alert("Ths domain name contains invalid characters.");
alert("Invalid Email id!");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

//alert("The username doesn't valid.");
alert("Invalid Email id!");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
 alert("Invalid Email id!");
//alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("Invalid Email id!");
//alert("The domain name does not valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
	alert("Invalid Email id!");
//alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2)
{
	alert("Invalid Email id!");
//alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//URL validation Script
function validateurl(urlstr)
{
document.frm.txturl.value=urlstr;
var b = trim(document.frm.txturl.value);
if(b.match(/^http:\/\/\w+(\.\w+)*\.(\w{2}|com|net|org|mil|int|edu|gov|info|biz|coop|aero|pro|name|museum)(\/[\w\-\.])*$/) == null)
  {
       alert("Invalid URL.");
       
  }
  
}


