/******************************************
Module name : Js Function file
Parent module : None
Date created : 9th June 2008
Date last modified : 10th June 2008
Author :  Gulshan Verma
Last modified by : Gulshan Verma
Comments : The functions_js.js file contains various functions related to the web directory project.
******************************************/	
/******************************************
Function name : validator
Return type : boolean
Date created : 9th June 2008
Date last modified : 10th jun 2008
Author : Gulshan Verma
Last modified by : Gulshan Verma
Comments : Function will return the true or error message after validating the entire form
User instruction : validateForm()
******************************************/
var id;
function validateForm() 
{ 
		var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
		var focus_flag = false;
		j=0;
		//	/^([-a-zA-Z0-9._]+@[-a-zA-Z0-9.]+(\.[-a-zA-Z0-9]+)+)$/;
		var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var regBlank = /[^\s]/;
		var regSpace = /[\s]/;
		//var regAlphaNum = /^([a-zA-Z0-9-/_ :;#!@\n\r.,$*&%?^~`=+(){}\[\]\"\'\\]+)$/;
		var regAlphaNum = /^([a-zA-Z0-9_]+)$/;
		var regDate = /^([0-9_]+-[0-9][0-9]+-[0-9][0-9]+)$/; 
		var regChar = /^([a-zA-Z]+)$/;
		var elem, i=0 , attr = 'emsg' , attr1 = 'alt' , attr2='name' ;
		while ( elem = document.forms[""+args[0]].elements[i++] )
		{
			
			mesg	= ( elem[attr] ) ? elem[attr] : elem.getAttribute( attr );
			test	= ( elem[attr1] ) ? elem[attr1] : elem.getAttribute( attr1 );			
			element = ( elem[attr2] ) ? elem[attr2] : elem.getAttribute( attr2 );									
			
			val=document.forms[""+args[0]].elements[""+element];
			
		    if (val && test && mesg) 
			{	
				nm=mesg; 
				noVal = val;
				val = val.value;
			if(regBlank.test(val))
			{
				if(test.indexOf('isEqual')!=-1)
				{
					result = trim(val);
				if(result.length==0)
				{
				errors += '- '+nm+' is required.\n'; 
				}else{
					
					equal_obj_val = test.substring(8,test.indexOf(":"));
					mesg_string =test.substring((test.indexOf(":")+1));

					if(val != document.forms[""+args[0]].elements[""+equal_obj_val].value)
					{ 
						errors+='- '+nm+' and '+mesg_string+' must be same.\n';
					}
				}
				}
				else if(test.indexOf('isAlphaNum')!=-1)
				{
				result = trim(val);
				if(result.length==0){
				errors += '- '+nm+' is required.\n'; 
				}else{
					if(!regAlphaNum.test(val))
					{
						//errors += '- '+nm+' is not valid.\n';
						errors+='- '+nm+': Special characters are not allowed.\n';
					}
				}
			}
				else if(test.indexOf('isSpace')!=-1)
				{
				result = trim(val);
				
				if(result.length==0)
				{
					errors += '- '+nm+' is required.\n'; 
				}
				else
				{
					if(regSpace.test(val))
					{
						errors += '- '+nm+' is not valid.\n';
					}
				
				}
				}
				else if (test.indexOf('isDate')!=-1) 
				{ 
					p=val.indexOf('-');
			       // alert(test.indexOf('isDate'));
			       	var sliptdate	= val.split("-");
					
					/*******************Added by rupesh Date is not before current date and month*********************/
					var today=new Date(),TY=today.getFullYear(),TM=today.getMonth(),TD=today.getDate(),TH=today.getHours();
					TM+=1;			
					if(TM<=9) 
					{	
						TM='0'+TM;
					}
					if(TD<=9)
					{
						TD='0'+TD;
					}
					/*******************Added by rupesh*********************/
					
					var sY=sliptdate[0];
					var sM=sliptdate[1];
					var sD=sliptdate[2];
					
					/*******************Added by rupesh*********************/
					//alert(TM);
					if(sY<TY ) {
						
						errors+='- '+nm+' should be greater than current date.\n';
					}
					else if(sM==TM && sD<TD && sY==TY) { 

						errors+='- '+nm+' should be greater than current date.\n';
						
					}
					else if(sM<TM && sY==TY) { 

						errors+='- '+nm+' should be greater than current date.\n';
						
					}
					
					/*************************Added by rupesh********************/
					else if (p != 4 || sY.length != 4 || sM.length != 2 || sD.length != 2 )
					{
						errors+='- '+nm+' must contain Valid Date YYYY-MM-DD.\n';
		
					}
					else if(!regDate.test(val))
					{
						errors+='- '+nm+'  must contain Valid Date YYYY-MM-DD.\n';
					}
					else if(sM>12 || sD>31 || sY<2006)
					{
						errors+='- '+nm+'  must contain Valid Date YYYY-MM-DD.\n';
					}
			     }
				else if (test.indexOf('isBackDate')!=-1) 
				{ 
					p=val.indexOf('-');
			       // alert(test.indexOf('isDate'));
			       	var sliptdate	= val.split("-");
					
					/*******************Added by Gulshan Date is not after current date and month*********************/
					var today=new Date(),TY=today.getFullYear(),TM=today.getMonth(),TD=today.getDate(),TH=today.getHours();
					TM+=1;			
					if(TM<=9) 
					{	
						TM='0'+TM;
					}
					if(TD<=9)
					{
						TD='0'+TD;
					}
					/*******************Added by Gulshan*********************/
					var sY=sliptdate[0];
					var sM=sliptdate[1];
					var sD=sliptdate[2];
					/*******************Added by Gulshan*********************/
					if(sY>TY ) {
						
						errors+='- '+nm+' should be lesser than current date.\n';
					}
					else if(sM==TM && sD>TD && sY==TY) { 

						errors+='- '+nm+' should be lesser than current date.\n';
						
					}
					else if(sM>TM && sY==TY) { 

						errors+='- '+nm+' should be lesser than current date.\n';
						
					}
					
					/*************************Added by rupesh********************/
					else if (p != 4 || sY.length != 4 || sM.length != 2 || sD.length != 2 )
					{
						errors+='- '+nm+' must contain Valid Date YYYY-MM-DD.\n';
		
					}
					else if(!regDate.test(val))
					{
						errors+='- '+nm+'  must contain Valid Date YYYY-MM-DD.\n';
					}
					else if(sM>12 || sD>31 || sY<2006)
					{
						errors+='- '+nm+'  must contain Valid Date YYYY-MM-DD.\n';
					}
			     }
				else if(test.indexOf('isValidCreditCardNumber')!=-1) 
				{
					if(chkCardDetails())
					{
						focus_flag = true;
						credit_card_errors = chkCardDetails();
						errors+=credit_card_errors;
					}
				}
				else if (test.indexOf('isEmail')!=-1) 
				{ 

					p=val.indexOf('@');
					s=val.indexOf('.');
			        if (p<1 || p==(val.length-1))
					{
						errors+='- '+nm+' must contain an e-mail Address.\n';
		
					}
					//else if(s<p || s==(val.length-1))
					else if(!regEmail.test(val))
					{
						errors+='- '+nm+' must contain a valid e-mail Address.\n';
					}
			     }
				else if (test.indexOf('isUrl')!=-1) 
				{ 
					p=val.indexOf('http://');
					s=val.indexOf('.');
			        if (p<0 || p==(val.length-1))
					{
						errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
		
					}
					else if(s<p || s==(val.length-1))
					{
						errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
					}
			     }
				 else if(test.indexOf('isMp3')!=-1)
				 {
					  
					function reverse(val) 
					{	
						var length = val.length;
						var letters = new Array(length);
						var letters2 = new Array(length);
						var backword = "";
						for (i=0; i<=length-1; i++) 
						{
							letters[i] = val.substring(i, i+1);
						}
						f = 0
						for (j=i-1; j>=0; j--) 
						{
							letters2[f] = letters[j];
							f++;
						}
						for (h=0; h<=letters2.length-1; h++) 
						{
							backword +=letters2[h];
						}
					 	return backword;
					}
					var back = reverse(val);
					var ValExt	= back.split(".");
					var ValExt = reverse(ValExt[0]);
					
					if (ValExt == "mp3" || ValExt == "MP3") 
					{
						flag="yes";
					} 
					else 
					{
						flag="no";						
					} 
					if(flag=="no")
					{
						errors+='- '+nm+' must be (mp3).\n';						
					}
				 
				 }
				 else if (test.indexOf('isChar')!=-1) 
				 { 
					var first_char;
					
					if(val.match(regChar)==null)
					{
					 	errors+='- '+nm+' must contain a character.\n';
					}
			     }
				 else if(test.indexOf('isCheckbox')!=-1)//Check is check box is not checked generate error
				{	
					var valueCheckbox = noVal.checked;
					if(!valueCheckbox)
					{
						errors+='- '+' Accept terms and Policy.\n';
					}

				}
	   			 else if (test!='R' || test=='Numbers') 
				 {
					 
				 result = trim(val);
					if(result.length==0){
						
						if(errors.indexOf('Phone') == -1)
					errors += '- '+nm+' is required.\n'; 
					}
					
				    if (isNaN(val)) 
					{
					 if(errors.indexOf('Phone') == -1)
					 errors+='- '+nm+' must contain a number.\n';
					}
					else
					{
					if(test=='Numbers' && !isNaN(val))
					{
					var i1;
					var num1=0;
						for (i1 = 0; i1 < val.length; i1++)
						{   
							// Check that current character is number.
							var c = val.charAt(i1);
							if (((c < "0") || (c > "9"))) num1=1;
						}
						if(num1==1)
						{
							if(errors.indexOf('Phone') == -1)
							errors+='- '+nm+' can only contain numbers.\n';
						}
					}
					}
					if (test.indexOf('inRange') != -1) 
					{ 
						num = parseFloat(val);
						p=test.indexOf(':');
						min=test.substring(8,p); 
						max=test.substring(p+1);
						if (num<min || max<num) 
							if(min==max)
							{
							  if(min==1 && max==1 & num==0)
								{
									errors+='- '+nm+' should  be more than '+min+'.\n';
							    }
								if(min!=1 && max!=1 && min==max)
								{
									errors+='- '+nm+' should not be more than '+min+'.\n';
								}
								
							}				
							
						else
						{
						errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
						}
					} 
					if (val.indexOf('-') != -1) 
					{ 
						errors='- '+nm+' must contain a number.\n';
					} 
					if (val.indexOf('+') != -1) 
					{ 
						errors='- '+nm+' must contain a number.\n';
					}
				}
				else if (test.charAt(0)=='R')
				{
					result = trim(val);
					if(result.length==0)
					{
						errors += '- '+nm+' is required.\n'; 
					}
				} 
			}
			else if (test.charAt(0) == 'R' || test=='Numbers')
			{
				if(errors.indexOf('Phone') == -1 && test=='Numbers')
				{
				errors += '- '+nm+' is required.\n'; 
				}
				if (test.charAt(0) == 'R')
				{					
				errors += '- '+nm+' is required.\n'; 	
				}
			}
		}
		if(errors !="")
		{	
			if(j<=0 )
			{
				focusitem =document.forms[""+args[0]].elements[""+element];
				j++;
			}	
		}
	} 
	if (errors)
	{
		var MasterString = getMasterString();
		alert(MasterString+'\n'+errors);
		if(!focus_flag)
		{
			focusitem.focus();
		}
		return false;
	}
	else
		return true;
	document.MM_returnValue = (errors == '');
}
/******************************************
Function name : getMasterString
Return type : boolean
Date created : 9th June 2008
Date last modified : 10th June 2008
Author : Gulshan Verma
Last modified by : Gulshan Verma
Comments : Function will return the main string
User instruction : getMasterString()
******************************************/
function getMasterString()
{
	return "Sorry, we can not complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n";
}
/******************************************
Function name : ltrim
Return type : string
Date created : 9th June 2008
Date last modified : 10th June 2008
Author : Gulshan Verma
Last modified by : Gulshan Verma
Comments : Function will return the main string after removing white spaces from the left
User instruction : ltrim(str)
******************************************/
function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
/******************************************
Function name : rtrim
Return type : string
Date created : 9th June 2008
Date last modified : 10th June 2008
Author : Gulshan Verma
Last modified by : Gulshan Verma
Comments : Function will return the main string after removing white spaces from the right
User instruction : rtrim(str)
******************************************/
function rtrim(str) {

	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
/******************************************
Function name : trim
Return type : string
Date created : 9th June 2008
Date last modified : 10th June 2008
Author : Gulshan Verma
Last modified by :Gulshan Verma
Comments : Function will return the main string after removing white spaces from the right and left of the main string
User instruction : trim(str)
******************************************/
function trim(str) {
	return ltrim(rtrim(str));
}
/******************************************
Function name : isWhitespace
Return type : integer
Date created : 9th June 2008
Date last modified : 10th June 2008
Author : Gulshan Verma
Last modified by : Gulshan Verma
Comments : Function will return the index of white space encounter in the string.
User instruction : isWhitespace(charToCheck)
******************************************/
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function hidedisplayForm()
{
	document.getElementById('fgtFrm').style.display= 'block';
	document.getElementById('loginfrm').style.display= 'none';
	
}
function backToLogin()
{
	
	document.getElementById('fgtFrm').style.display= 'none';
	document.getElementById('loginfrm').style.display = 'block';
	
}

function fob(x){
	return document.getElementById(x);
}
function get_list(){

	x=document.getElementById('To').value
	url="ajax.php";
	WhereToWrite="Layer2";
	id = WhereToWrite;
	
//	alert(WhereToWrite);
	fields="uid="+x+"&action=importList";
	
//	makeRequest(url,WhereToWrite,fields);
	doAjax('ajax.php',fields,'showAllList','post',2);
	//fob('To').value="";
return false;
}
function showAllList(item)
{
	
		document.getElementById(id).style.display='';
		document.getElementById(id).innerHTML=item;
}
function set_list(uid,action){

	document.getElementById('To').value="";
	list_id=x=fob('uid_list').value;
	x=uid;
	url="ajax.php";
	WhereToWrite="td_ID";
	id = WhereToWrite;
	fields="list_id="+list_id+"&uid="+x+"&action="+action;
//	makeRequest(url,WhereToWrite,fields);
	doAjax('ajax.php',fields,'SetAllList','post',2);
return false;
}
function SetAllList(item)
{
	

	document.getElementById('Layer2').style.display='none';
	document.getElementById(id).style.display='';
	document.getElementById(id).innerHTML=item;
}
function delete_list(uid,action){
	list_id=x=fob('uid_list').value;
	x=uid;
	url="ajax.php";
	WhereToWrite="td_ID";
	id = WhereToWrite;
	
	fields="list_id="+list_id+"&uid="+x+"&action="+action;
	doAjax('ajax.php',fields,'deleteList','post',2);
	return false;
}
function deleteList(item)
{

	document.getElementById(id).style.display='';
	document.getElementById(id).innerHTML=item;


}

function set_focus(){
	fob('To').focus();
return false;	
}

function hide_div(div_id){
fob(div_id).style.display='none';
return false;
}

