//**********************************************************************************************************************//
//																														//
//	Form Validation [ Version : AS-1.0/2004 ].																			//
//																														//
//	Function:	thisformvalidation.																						//
//																														//
//	If you want to use this Form Validation funcion, make sure that each and every field must have						// 		
//	the parameter "required",and it should have the value of 'R' ,'RinRange', 'RisEmail', 'inRange', 					//
//	'isEmail', or 'RisDate YYYY-MM-DD', or 'RisDate DD-MM-YYYY', or 'No'.																									//
//																														//
//	Here the first letter ('R') of each case indicates mandatory Fields.												//
//																														//
//**********************************************************************************************************************//

function thisformvalidation( form ){
	
	p=0; e=0; u=0; cnt=0; ecnt=0; errors=""; pass1 = ""; pass2 = ""; email1=""; email2 = ""; v="false";
	len=form.elements.length;
	var focus_obj = "";
	for(i=0;i<len;i++){
	
		dfe = form.elements[i];
		if(dfe.errname){
			nm = dfe.errname;
		}else{
			nm = dfe.name;
		}
		
		fieldname = dfe.name;
		
		if(dfe.required){
			if(dfe.value=="" || dfe.value.charAt(0) ==" "){ //If 2 Starts ----- If the current checking field having the value or not?.
			
				if(dfe.required.charAt(0) == 'R'){
					errors += '- '+nm+' is must required.\n';
					if(focus_obj==""){focus_obj = dfe;}
				}
					
			}else if(dfe.type=="checkbox"){ 
			
				if(dfe.required.charAt(0) == 'R' && !dfe.checked){
					errors += '- '+nm+' must be accepted.\n'; 
					if(focus_obj==""){focus_obj = dfe;}
				}
			}else if(dfe.value!=""){ 
				
				if(dfe.value.charAt(0) == " "){
					errors += '- '+nm+' must not contain blank space at first.\n'; 
					if(focus_obj==""){focus_obj = dfe;}
				}else{
					valid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.";
					
					if(fieldname.indexOf("pass") != -1){//If 4 Starts   ----- If the current checking field name is part of "pass"
						
						if(dfe.name == "password"){//If 5 Starts
							++cnt;
							pass1=dfe.value;
						}else if(dfe.name == "confirmpassword"){
							++cnt;
							pass2=dfe.value;
						}//If 5 Ends
						
						if(dfe.value != ""){//If 6 Starts  -----  If the current checking field is not empty
							
							if(dfe.value.length < 5){
								errors += '- '+nm+' must contain 5 characters!\n';
								if(focus_obj==""){focus_obj = dfe;}
							}else if(dfe.value.length >= 5){//If 7 Starts
								
								len1 = dfe.value.length;
								vv = dfe.value;
								pflag=false;char=0;num=0;wpass=0;
								
								for(ff=0;ff<len1;ff++){//For 3 Starts
									
									char = vv.charAt(ff);
									
									if(valid.indexOf(char) == -1){//If 8 Starts
										
										pflag=true;
										++wpass;
										
									}//If 8 Ends
									
								}//For 3 Ends
								
								if(wpass!=0){
									if(focus_obj==""){focus_obj = dfe;}
									errors += '- '+nm+' should not contain special characters!\n';
								}
							}//If 7 Ends
							
						}//If 6 Ends
						
						if(cnt == 2){//If 10 Starts
							
							++cnt;
							
							if(pass1!="" && pass2!="" && (pass1!=pass2)){
								if(focus_obj==""){focus_obj = dfe;}
								errors+='- Password & the Confirm Password should be same!\n';
							}
						}//If 10 Ends
						
					}//If 4 Ends
					
					test=dfe.required;
					val= dfe.value;
					
					if (test.indexOf('isDate')!=-1) { //If 11 Starts  -----  If the current checking field is email
						arr_dateformat = test.split("|");
						//alert(arr_dateformat)
						//alert(arr_dateformat[3]);
						checkprevi = true;
						if(arr_dateformat[2])
							if(arr_dateformat[2] == "No")
								checkprevi = false;
							else
								checkprevi = true;
								
						dateformat = "YYYY-MM-DD"
						if(arr_dateformat[1]){
							dateformat = arr_dateformat[1];
							if(arr_dateformat[1] == "DD-MM-YYYY"){
								var dateFormat = /^\d{2}(\-)\d{2}\1\d{4}$/
								var m = dateFormat.test(val);
							}else{
								var dateFormat = /^\d{4}(\-)\d{2}\1\d{2}$/
								var m = dateFormat.test(val);
							}
						}else{
							var dateFormat = /^\d{4}(\-)\d{2}\1\d{2}$/
							var m = dateFormat.test(val);
						}
						
						if (m == false) {
							errors += '- '+nm+' should contain a valid date('+dateformat+')!.\n';
							if(focus_obj==""){focus_obj = dfe;}
						}else{
							
							var arr_date = val.split("-");
							var arr_months = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
							
							var today = new Date();
							var toyear = today.getYear();
							var tomonth = today.getMonth()+1;
							var todate = today.getDate();
							//alert(arr_date)
							var year = parseInt(arr_date[0]);
							var month = parseInt(arr_date[1]);
							var date = parseInt(arr_date[2]);
							//alert(year+":"+month+":"+date)
							if(arr_dateformat[1]){
								if(arr_dateformat[1] == "DD-MM-YYYY"){
									year = parseInt(arr_date[2]);
									date = parseInt(arr_date[0]);
								}
							}
							arr_months[2] = isLeap(year)?29:28;
							
							todate = toyear+""+((tomonth<10)?"0"+parseInt(tomonth):tomonth)+""+((todate<10)?"0"+parseInt(todate):todate);
							seldate = year+""+((month<10)?"0"+parseInt(month):month)+""+((date<10)?"0"+parseInt(date):date);
							
							age=(toyear-year);
							
							if( (parseInt(seldate)<parseInt(todate)) && (checkprevi == true) ){
								errors += '- '+nm+' should contain a valid "Future Date" ('+dateformat+')!.\n';if(focus_obj==""){focus_obj = dfe;}
							}else if(parseInt(month)>12||parseInt(month)<1){
								errors += '- '+nm+' should contain a valid date('+dateformat+')!.\n';if(focus_obj==""){focus_obj = dfe;}
							}else if(parseInt(arr_months[month])<parseInt(date)){
								errors += '- '+nm+' should contain a valid date('+dateformat+')!.\n';if(focus_obj==""){focus_obj = dfe;}
							}else if(arr_dateformat[3]!=""){
									if(age < arr_dateformat[3]){
										errors += '- '+nm+' should be above '+16+' Years!.\n';if(focus_obj==""){focus_obj = dfe;}
									}
							}
						}
					}
					
					if (test.indexOf('isURL')!=-1) { //If 11 Starts  -----  If the current checking field is email
						var re = /^(http|ftp|https):\/\/[A-Za-z0-9-]+\.[A-Za-z0-9-]+\.[A-Za-z0-9-]+/; 
						var m = re.test(val);
						if (m == false) {
							errors += '- '+nm+' should contain a valid URL!.\n';if(focus_obj==""){focus_obj = dfe;}
						}
					}
					
					if (test.indexOf('isEmail')!=-1) { //If 11 Starts  -----  If the current checking field is email
						
						//Email Validation
						
						if(dfe.name == "adminemail"){//If 5 Starts
							++ecnt;
							email1=dfe.value;
						}else if(dfe.name == "confirmemail"){
							++ecnt;
							email2=dfe.value;
						}//If 5 Ends
						
						flag = false;
							
						validRegExp = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}$/i;
						if (val.search(validRegExp) == -1) 
						{flag = false;						
						}else{
						flag = true;		
						}
							
						if(flag == false){
							errors+='- '+nm+' must contain an e-mail address.\n';
							if(focus_obj==""){focus_obj = dfe;}
						}
						if(ecnt == 2){//If 10 Starts
							++ecnt;
							if(email1!="" && email2!="" && (email1!=email2)){
								errors+='- Email & the Confirm Email should be same!\n';
								if(focus_obj==""){focus_obj = dfe;}
							}
						}//If 10 Ends
							
					}
					//pradeep
					//field with full of characcters
					if (test.indexOf('fullchar') != -1) { 
					valid = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/\"'-.&@#()+,_";
					len1 = dfe.value.length;
					vv = dfe.value;
								pflag=false;char=0;num=0;wchar=0;
								
								for(ff=0;ff<len1;ff++){//For 3 Starts
									
									char = vv.charAt(ff);
									
									if(valid.indexOf(char) == -1){//If 8 Starts
										
										pflag=true;
										++wchar;
										
									}//If 8 Ends
							}//For 3 Ends
								
						if(wchar!=0){
							if(focus_obj==""){focus_obj = dfe;}
							errors += '- '+nm+' should contain only characters!\n';
						}
					}
					//FIELD WITH CHARACTERS AND SPACE
					if (test.indexOf('spacechar') != -1) { 
					valid = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/\"'-.&@#()+,_";
					len1 = dfe.value.length;
					vv = dfe.value;
								pflag=false;char=0;num=0;wchar=0;
								
								for(ff=0;ff<len1;ff++){//For 3 Starts
									
									char = vv.charAt(ff);
									
									if(valid.indexOf(char) == -1){//If 8 Starts
										
										pflag=true;
										++wchar;
										
									}//If 8 Ends
							}//For 3 Ends
								
						if(wchar!=0){
							if(focus_obj==""){focus_obj = dfe;}
							errors += '- '+nm+' should not contain any special character or numbers!.\n';
						}
					}
					//FIELD WITH CHAR AND NUMVERS
					if (test.indexOf('NumChar') != -1) { 
						valid = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/\"'-.&@#()+,_";
						len1 = dfe.value.length;
						vv = dfe.value;
						pflag=false;char=0;num=0;wchar=0;
						
						for(ff=0;ff<len1;ff++){//For 3 Starts
							
							char = vv.charAt(ff);
							
							if(valid.indexOf(char) == -1){//If 8 Starts
								//alert(char);
								pflag=true;
								++wchar;
								
							}//If 8 Ends
						}//For 3 Ends
								
						if(wchar!=0){
							if(focus_obj==""){focus_obj = dfe;}
							errors += '- '+nm+' should contain only characters/numbers!.\n';
						}
					}
					//FIELD CHECK FOR IMAGE
					if (test.indexOf('isImage') != -1) { 
						if(dfe.value != " "){
							var ext = dfe.value;
							ext = ext.substring(ext.length-3,ext.length);
							ext = ext.toLowerCase();
							   if ((ext != 'jpg') && (ext !='png') && (ext !='gif')){
									if(focus_obj==""){focus_obj = dfe;}
									errors += '- '+nm+' should contain one of "jpg" or "png" or "gif" Image file!.\n';
								}
						}
					}
					//FIELD CHECK FOR NUMBERS
					if (test.indexOf('NUM') != -1) { 
					valid = " 0123456789.";
					len1 = dfe.value.length;
					vv = dfe.value;
								pflag=false;char=0;num=0;wchar=0;
								if(vv.charAt(0) == 0 || vv.charAt(0) == "."){//If 8 Starts
									pflag=true;
									++wchar;
								}else{
									for(ff=0;ff<len1;ff++){//For 3 Starts
										
										char = vv.charAt(ff);
										
										if(valid.indexOf(char) == -1){//If 8 Starts
											
											pflag=true;
											++wchar;
											
										}//If 8 Ends
									}//For 3 Ends
								}
								
						if(wchar!=0){
							if(focus_obj==""){focus_obj = dfe;}
							errors += '- '+nm+' should contain only valid numbers!\n';
						}
					}
					
					//FIELD CHECK FOR NUMBERS
					if (test.indexOf('ONUM') != -1) { 
					valid = "0123456789.";
					len1 = dfe.value.length;
					vv = dfe.value;
						pflag=false;char=0;num=0;wchar=0;
						char1 = vv.charAt(0);
						if(vv.charAt(0) == 0){//If 8 Starts
							pflag=true;
							++wchar;
						}else{
							for(ff=0;ff<len1;ff++){//For 3 Starts
								char = vv.charAt(ff);
								if(valid.indexOf(char) == -1){//If 8 Starts
									pflag=true;
									++wchar;
								}//If 8 Ends
							}//For 3 Ends
						}
							//alert(errors)	
						if(wchar!=0){
							if(focus_obj==""){focus_obj = dfe;}
							errors += '- '+nm+' should contain only valid numbers!\n';
						}
					}
					
					if (test.indexOf('MAXICHECK') != -1) {
						vallen = val.length;
						//alert(val);
						if(vallen>150){
							if(focus_obj==""){
								focus_obj = dfe;
							}
							errors += '- '+nm+' should contain maximum of 150 characters!.\n';
						}
					}
					if (test.indexOf('MAXIDESC') != -1) {
						vallen = val.length;
						//alert(val);
						if(vallen>35){
							if(focus_obj==""){
								focus_obj = dfe;
							}
							errors += '- '+nm+' should contain maximum of 35 characters!.\n';
						}
					}


					if (test.indexOf('Onlynumber') != -1) { 
					valid = "0123456789.";
					len1 = dfe.value.length;
					vv = dfe.value;
						pflag=false;char=0;num=0;wchar=0;
						char1 = vv.charAt(0);
						if(vv.charAt(0) == 0){//If 8 Starts
							pflag=true;
							++wchar;
						}else{
							for(ff=0;ff<len1;ff++){//For 3 Starts
								char = vv.charAt(ff);
								if(valid.indexOf(char) == -1){//If 8 Starts
									pflag=true;
									++wchar;
								}//If 8 Ends
							}//For 3 Ends
						}
								
						if(wchar!=0){
							if(focus_obj==""){focus_obj = dfe;}
							errors += '- '+nm+' should contain only valid numbers!\n';
						}
					}
					//pradeep
					if (test.indexOf('inRange') != -1) { //If 12 Starts  -----   If the current checking field is only in numeric
						if( test.indexOf(':') != -1 ){
							num = parseFloat(val);
							p=test.indexOf(':');
							min=test.substring(8,p); 
							max=test.substring(p+1);
							if (num<min || max<num){
								errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
								if(focus_obj==""){focus_obj = dfe;}
							}
						}else{
							valid = "0123456789-+. ()";
							lens = val.length;
							/*if(lens <=6){
								errors += '- '+nm+' should contain minimum of 6 number! ';
								if(focus_obj==""){focus_obj = dfe;}
							}*/
							dfevalue = dfe.value;
							char=0;num=0;valcnt=0;
							for(idx=0;idx<lens;idx++){//For 3 Starts
								char = dfevalue.charAt(idx);
								if(valid.indexOf(char) == -1){//If 8 Starts
									++valcnt;
								}//If 8 Ends
							}//For 3 Ends
							if(valcnt!=0){
								errors += '- '+nm+' should contain valid number!.\n';
								if(focus_obj==""){focus_obj = dfe;}
							}
						}
					} //If 12 Ends
				}
			}
		}
	}
	if (errors){ //  ----- If not empty of error Starts

		alert('The following error(s) occurred:\n\n'+errors);
		focus_obj.focus();
		return false;
		
	}//  ----- If not empty of error ends

	return true;
}

// ----------------------------------------------------------------------------------------------------------------------------

function isLeap(year){
	if (year % 4 == 0){
		return true;
	}
	return false;
}

// ----------------------------------------------------------------------------------------------------------------------------
// XTRA Actions 
// ----------------------------------------------------------------------------------------------------------------------------
	var xtra_debug_mode = false;
	var xtra_request_type = "POST";
	
	function xtra_debug(text) {
		if (xtra_debug_mode)
			alert("RSD: " + text)
	}
	
	function xtra_init_object() {
		xtra_debug("xtra_init_object() called..")
		
		var A;
		try {
			A=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				A=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				A=null;
			}
		}
		if(!A && typeof XMLHttpRequest != "undefined")
			A = new XMLHttpRequest();
		if (!A)
			xtra_debug("Could not create connection object.");
		return A;
	}
	
	function xtra_do_call(func_name, args) {
	
		var i, x, n;
		var uri;
		var post_data;
		uri = "xtra.php";
		
		if (xtra_request_type == "GET") {
			if (uri.indexOf("?") == -1) 
				uri = uri + "?actions=" + escape(func_name);
			else
				uri = uri + "&actions=" + escape(func_name);
			for (i = 0; i < args.length-1; i++) 
				uri = uri + "&xtraargs[]=" + escape(args[i]);
			uri = uri + "&rsrnd=" + new Date().getTime();
			post_data = null;
		} else {
			post_data = "actions=" + escape(func_name);
			for (i = 0; i < args.length-1; i++) 
				post_data = post_data + "&xtraargs[]=" + escape(args[i]);
		}
		
		x = xtra_init_object();
		x.open(xtra_request_type, uri, true);
		if (xtra_request_type == "POST") {
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		
		x.onreadystatechange = function() {
			if (x.readyState != 4) 
				return;
			xtra_debug("received " + x.responseText);
			
			var status;
			var data;
			status = x.responseText.charAt(0);
			data = x.responseText.substring(2);
			if (status == "-") 
				alert("Error: " + data);
			else  
				args[args.length-1](data);
		}
		x.send(post_data);
		xtra_debug(func_name + " uri = " + uri + "/post = " + post_data);
		xtra_debug(func_name + " waiting..");
		delete x;
	}
	
	function x_SendComments() {
		document.getElementById("innerform").innerHTML = "<center>Please wait...</center>";
		xtra_do_call("sendcomments",x_SendComments.arguments);
	}
	
	function getSendComments_cb(results){
		document.getElementById("innerform").innerHTML = results;
	}
	
	var friendsPage = 0;
	
	// ----------------------------------------------------------------------------------------------------------------------------
	// XTRA Actions 
	// ----------------------------------------------------------------------------------------------------------------------------
	
	function checkgoto(form,tot){
		if(form.goto.value==""){
			alert("You must enter the page number!.");
			form.goto.focus();
			return false;
		}else{
			goto = form.goto.value;
			if((!isNaN(goto)) && (goto > 0) && (parseInt(goto) <= tot) && goto.charAt(0)!=" "){}
			else{
				alert("You must enter a valid page number!.\nRange of (1 to "+tot+")");
				form.goto.value="";
				form.goto.focus();
				return false;
			}
		}
	}
	
	function setgoto(page){
		document.paging.goto.value=page;
	}
	
	function details( id ){
		disp = document.getElementById(id).style.display;
		if(disp == "none")
			display = "block";
		else
			display = "none";
			
		document.getElementById(id).style.display = display;
	}
	
	function visibility( id ){
		disp = document.getElementById(id).style.display;
		if(disp == "none"){
			document.getElementById(id).style.display = "block";
			document.getElementById("menus").style.display = "none";
		}else{
			document.getElementById(id).style.display = "none";
			document.getElementById("menus").style.display = "block";
		}
	}
	
	ChildWindow = null;
	
	function FocusSub(){
		if (ChildWindow != null){
			if (!ChildWindow.closed)
				ChildWindow.focus();
		}
	}
	
	function openwindow( openURI ){
		var W = 780;
		var H = 524;
		var X = (((screen.width)/2)-(W/2));
		var Y = (((screen.height)/2)-(H/2));
		var s="left="+X+",top="+Y+",width="+W+",height="+H;
		var popup = window.open(openURI,'windowName',s);
		if (!popup.opener) popup.opener = self
		return true;
	}

	function setHome(){
	   document.body.style.behavior='url(#default#homepage)';
	   document.body.setHomePage(window.location.href);
	}
	
	function getObj(id,d){
		var i,x;  if(!d) d=document; 
		if(!(x=d[id])&&d.all) x=d.all[id]; 
		for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][id];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getObj(id,d.layers[i].document);
		if(!x && document.getElementById) x=document.getElementById(id); 
		return x;
	};

