//---- V A L I D A T I O N    F U N T I O N  
// B - Blank , N - Number field ,  T- Text field , E- email check, P - password
function mf_validation(obj,type,msg) 
{ 
     // removing white spaces
	  if (type!= "I") 
  	  { 
	    obj.value = trim(obj.value);
	  }

    // Blank Field Validation
			if (type=="B") 
			{ 
		 
				if(obj.value=="") 
				{ 
					alert(msg); 
					obj.select();
					obj.focus(); 
					return false; 
				} 
			} // end if type B 

		// Number Field Validation
			if (type=="N") 
			{ 
				if(obj.value=="" || obj.value<0 || isNaN(obj.value)==true) 
				{ 
					alert(msg); 
					obj.select();
					obj.focus(); 
					return false; 
				} 
 
			} // end if type N
			
		
 		// Text Field Validation
			if (type=="T") 
			{ 
				var last = obj.value;
				var flag = 1;
				for (var i = 0; i < last.length; i++)
				{
					var ch = last.substring(i, i + 1);
					if (((ch < "a" || "z" < ch) &&  (ch!=" " && ch!=",")) && ((ch < "64" || "Z" < ch) &&  (ch!=" " && ch!=",")))
						{
							flag = 0;
							break;
						}
				}
				if( flag==0 )
				 {
					alert(msg); 
					obj.select();
					obj.focus();
					return false;
				 }
		   	} // end if type T 
			
			// Email Field Validation
			if (type=="E") 
			{ 
				if(emailCheck(obj.value)==false) 
					{ 
						alert(msg); 
						obj.select();
						obj.focus(); 
						return false; 
					} 
 
			} // end if type E 
			
			
			// URL Field Validation
			if (type=="U") 
			{ 
				if(isValidURL(obj.value)==false) 
					{ 
						alert(msg); 
						obj.select();
						obj.focus(); 
						return false; 
					} 
 
			} // end if type U 	
			
			
			// blank image upload validation
			if (type=="I") 
			{ 
				if (!/(\.(gif|jpg|jpeg|png))$/i.test(obj.value))
					{ 
						alert(msg); 
						obj.select();
						obj.focus(); 
						return false; 
					} 
 
			} // end if type I
		
			

 
} // END FUNCTION  


// function for phone no validation Starts *****************************************

function mf_phone(obj,msg) 
{ 

	//var echeck=/^[0-9]+([-][0-9])*[0-9]+/;
	if(!checkInternationalPhone(obj.value))
	{
		alert(msg); 	
		obj.select();		
		obj.focus(); 
		return false; 

	}
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// 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)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// function for phone no validation Ends *****************************************

// function for url validation
function isValidURL(url){ 
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
    if(RegExp.test(url)){ 
        return true;
    }else{ 
		return false;
    } 
} 


// function for email no validation
function emailCheck(str1) 
{ 
        var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/ 
	
    if(RegExp.test(str1)){ 
        return true;
    }else{ 
		return false;
    } 


}//end of function emailCheck


// audio / video file handelling
function videoPopup(url,type)
 {
 
 var media;
 if(type==1)
  media = "audio";
 else 
   media = "video";
 if(url=="#")
  alert("Sorry "+ media + " is not available");
 else
    window.open(url,'popup','width=620,height=450,top=100,left=220,resizable=0,scrollbars=1');   
 }


//  function for enter key form submission

function checkEnter(e,form)
{ 

	//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)
		form.submit() //submit the form
		return false
	}
	else
	{
		return true
	}

}


// JavaScript Document

function click_validation(e)
{
//	 alert('sdfs');
	//alert(event.button + navigator.appName  );
  if (navigator.appName == 'Netscape'
           && e.which == 3)
     {
      alert("Right Click is not allowded")
      return false;
      }
   else 
    {
      if (navigator.appName == 'Microsoft Internet Explorer'
          && event.button==2)
	   {
       alert("Right Click is not allowded")
		return false;
       }
   return true;
    }
}



// password Validation function
function checkPassword(objPass,objCpass,objUid)
 {
		var strPass = objPass.value;
		var strCpass = objCpass.value;
		var strUid = objUid.value;	
		
		
		var len1=strPass.length;
		var len2=strCpass.length;
		
		if(len1 < 6)
		{
		alert('password should be of atleast 6 characters');
		objPass.value="";
		objCpass.value="";
		objPass.focus();
		return false;
		}
		
		if(strUid == strPass)
		{
		alert('Password and UserID should not same');
		objPass.value="";
		objCpass.value="";
		objPass.focus();
		return false;
		}
		
		
		if(len1!=len2)
		{
		alert('confirm password not match');
		objCpass.value="";
		 objCpass.focus();
		return false;
		}
		
		if(strPass != strCpass)
		{
		alert('confirm password not match');
		objCpass.value="";
		objCpass.focus();
		return false;
		}	 

 }
 
 // password Validation
	
	
// trim functions
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


// function for add to favourites
function favit(heading,address)
 {
	if(window.sidebar)
	  {
		  window.sidebar.addPanel(document.title,location.href,''); // for mozilla, netscape
	  }
	 else
	  { 
		  window.external.AddFavorite(location.href,document.title); // for IE
	  }
 }
 
/* 
 
*/
// select all check boxes
function Checkall(form)
 { 
   for (var i = 0; i < form.elements.length; i++)
     {    
	  if(form.elements[i].disabled!=true)
	   { 
	     flag=1;
         eval("form.elements[" + i + "].checked = form.elements[0].checked");  
	   }
     } 
 } 
 
 
 // date validations
 function chk_year(obj,msg)
{
	var current_date = new Date();
    var current_year = current_date.getFullYear();
	var dt=obj.value;
    var arr=dt.split('/');
	if(arr[2] < current_year)
	{
		alert(msg);
		return false;
	}
	else 
		return true;
	

}
function chk_mon(obj,msg)
{
	var current_date = new Date();
    // getMonth() returns 0 to 11 for months	
    var current_mon = current_date.getMonth();
	var dt=obj.value;
    var arr=dt.split('/');
	if(arr[1] < (current_mon+1))
	{
		alert(msg);
	    return false;
	}
	else
	{
			return true;
	}

}

function chk_day(obj,msg)
{
	var current_date = new Date();
    var current_day = current_date.getDate();
    var dt=obj.value;
    var arr=dt.split('/');

	if(arr[0] < current_day)
	{
		alert(msg);
	    return false;
	}
	else
		return true;
}

function validTextFile(obj,msg)
 {
// blank image upload validation
			if (!/(\.(pdf|html|htm|php))$/i.test(obj.value))
					{ 
						alert(msg); 
						obj.select();
						obj.focus(); 
						return false; 
					} 
 
 }
 
 // resrincting text area characters limit
 
function chopText(elem, limit)
 {
	if(elem.value.length>=limit)
	 {
	//  alert("General information required upto 350 characters..."); 
	  elem.value=elem.value.substring(0,limit);
	 }	
	//document.frmupdateuser.cLeft.value=limit-elem.value.length;
 }
 
 
 
 // image validation function
 //image validation function
 var extArray = new Array(".jpg",".jpeg",".gif", ".png");

function image_validation(name1)
{
	

var file=name1;
var a=name1.split(".");
if(a[1]==undefined)
{
alert("Please upload proper photo.");
return false;
}
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit){}
else
{
alert("Please only upload files that end in types:  "
    + (extArray.join("  ")) + "\nPlease select a new "
    + "file to upload and submit again.");
    return false;
    }
	
}

// function for validating multiple emails
function multiEmail(obj, msg)
{
	var emails = trim(obj.value);
	var email  = emails.split(',');
	var retVal;
	for (var i = 0; i < email.length; i++)
	 {
	   email_add = trim(email[i]);	  
	   if(emailCheck(email_add) == false) 
		{ 
			alert(msg + ', Check email id ' + (i + 1)); 
			obj.select();
			obj.focus(); 
			return false; 
			break;
		} 	

     }
	
}

// function for avoiding bots to track emails
function botsMail(name, domain)
 {
	if(name!='' && domain!='') 
	 {
		var char = '@';
		document.write ("<a href=\"mailto:" + name + char + domain + "\"> Send eMail </a>");
	 }
 }

