/* ************************************************************************************************** */
/*								FILENAME					validation.js     								  */
/*								AUTHOR					Suresh							  */
/*								PROJECT					MatrimonyDiary.com					  */
/*								DESCRIPTION			form fields validation  related functional                    			  */
/* ************************************************************************************************** */

//general validation starts
	function validate(frn,fn,txt,type)	{
		fdvalue = Trim(eval('document.'+frn+'.'+fn+'.value'));
//empty validation
		if(type == 'E'){
			if(fdvalue == ""){
				alert(txt);
				eval('document.'+frn+'.'+fn+'.focus()');
				return false;
			}
		}
// Non Empty validation NE
		if(type == 'NE')	{
			if(fdvalue.length > 0){
			return true;
			}
		}
// check password length
		if(type == 'PL')	{
			if(fdvalue.length < txt)	{
				alert("Please enter atleast "+txt+" characters for Password.");
				eval('document.'+frn+'.'+fn+'.focus()');
				return false;
			}
		}
//check for alphabets
		if(type == 'A'){
			if(!(fdvalue.match(/^[a-zA-Z ]+$/)))	{
			alert("Name must contain only alphabets.");
			eval('document.'+frn+'.'+fn+'.focus()');
			return false;
			}
		}
		//check for alphabets
		if(type == 'AN'){
			if(!(fdvalue.match(/^[a-zA-Z0-9]+$/)))	{
			alert("Please enter Alphabets and Numbers only.");
			eval('document.'+frn+'.'+fn+'.focus()');
			return false;
			}
		}
//check for Number
		if(type == 'N'){
			if(!(fdvalue.match(/^[0-9]+$/))){
			alert("Please enter Numbers only.");
			eval('document.'+frn+'.'+fn+'.focus()');
			return false;
			}
		}
//general validation ends
// check for valid email
		if(type == "EM")	{
			if(!validate_emailaddress(fdvalue,true,false))	{
					alert("Please enter a valid e-mail ID.");
					eval('document.'+frn+'.'+fn+'.focus()');
					return false;
			}
		}
	}
//trim starts 
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

//trim ends
//mail starts here
function validate_emailaddress(addr,man,db){
	if (addr == '' && man) {
	   if (db) alert('Email ID is mandatory.');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('Email ID contains invalid characters.');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) alert("Email ID contains non ascii characters.");
		  return false;
	   }
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('Email ID must contain an @.');
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('Email ID must not start with @.');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('Email ID must contain only one @.');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('Email ID must contain a period in the domain name.');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in Email ID.');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('Period must not immediately precede @ in Email ID.');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('Two periods must not be adjacent in Email ID.');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('Invalid primary domain in Email ID.');
	   return false;
	}
	return true;
}
// mail  ends here
//Date format validation

	function validateUSDate( strValue ) {
  var objRegExp = /^\d{1,2}(\-)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) 
    var arrayDate = strValue.split(strSeparator); 
	var temp = arrayDate[0] ;
	arrayDate[0] = arrayDate[1];
	arrayDate[1] = temp ;
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}

//bad word abuse starts
function chekbadwords(fn,txtname) {

	var x,notes,indwords,notes,str;
	var status = new String();
	notes = eval('document.'+fn+'.'+txtname+'.value');
	var badwordsArr=new Array(' \@\$\$ ', ' a\$\$ ', 'anton','arse','arsehole','ass','assmuncher','asshole','asstooling','asswipe','b\!tch','b17ch','b1tch','bastard','beefcurtins','bi7ch','bitch','bitchy','boiolas','bollocks','breasts','boob','brownnose','bugger','damn','buttpirate','c0ck','cawk','chink','clitsaq','cock','cockbite','cockgobbler','cocksucker','cum','cunt','dago','daygo','dego','dick','dickwad','dickhead','dickweed','douchebag','dziwka','ekto','enculer','faen','fag','faggot','fart','fatass','feg','felch','ficken','fitta','fitte','flikker','fok','fuck','fuck','Fuck','FUCK','fucker','f.u.c.k.','fucking','fuckwit','fuk','fuking','futkretzn','fux0r','gay','gook','h0r','handjob','helvete','honkey','hore','hump','injun','intercourse','kawk','kike','knulle','kraut','kuk','kuksuger','kurac','kurwa','langer','masturbation','merd','motherfucker','motherfuckingcocksucker','motherfuckingcocksucker','mutherfucker','nepesaurio','nigga','nigger','nonce','nude','nutsack','one\-eyed\-trouser\-snake','penis','picka','pissant','pizda','politician','prick','puckface','pule','pussy','puta','puto','rape','rimjob','rubber','scheisse','schlampe','schlong','screw','shit','sexual','shiteater','shiz','skribz','skurwysyn','slut','spermburper','spic','spierdalaj','splooge','spunk','tatas','tits','tossthesalad','twat','unclefucker','vagina','vittu','votze','wank','wanka','wanker','wankers','wankstain','whore','wichser','wop','yed','shaadi\.com','shaaadi\.com');
	for(x in badwordsArr)	 {
		indwords = badwordsArr[x];
		//formatWord ="/"+badwordsArr[x]+"/i";
		formatWord ="/"+badwordsArr[x]+"/i";
		status = notes.match(eval(formatWord));
		if(status) {
			var arrtoStr = new String();
			arrtoStr = status.toString();
			var arrtoStrOrg = arrtoStr;
			arrtoStr = arrtoStr.toLowerCase();
			alert("Please avoid typing prohibited words.");
			higlightTxtInTextarea(txtname, arrtoStrOrg);
			return false;
		}
	}
}
function higlightTxtInTextarea(textareaId, highlightText) {
var ht = new String(highlightText + '');
var s = new String(document.getElementById(textareaId).value + '');
var startPos = s.indexOf(ht);
		if(startPos == -1) {
		return false;
		} 
		
document.getElementById(textareaId).focus();
document.getElementById(textareaId).setSelectionRange(startPos, startPos + ht.length);
return true; 
}//bad word abuse ends


//check the file upload path starts
/*function chkFilePath(fname,tname) { 
	var r = eval('document.'+fname+'.'+tname+'.value');
	var dname = r.substr(0,1);
	var scolon = r.substr(1,1);
	var bslash = r.substr(2,1);
	//var dot = r.indexOf(".");
	var tlength = r.length;
	var elength = tlength - 4;
		
	var x = "ok";
	if(!(dname.match(/^[A-Za-z ]+$/))) 
		x= "failed";
		if(!(scolon == ":") )
			x= "failed";
			if(!(bslash == '\\') )
				x= "failed";
		//		if(!(dot == elength))
			//		x= "failed";
	if(x== "failed") {
		alert("Please upload a valid file...");
		if(eval('document.'+fname+'.'+tname+'.focus()')) 
			return false;	
		else
			return false;
	} else 
		return true;
} */
//check the file upload path starts
