// JavaScript Document
// Created By Saurabh Goyal
// Saurabh@spoton.in

function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			if(alerttxt!='')
			alert(alerttxt);
			field.focus(); 
			return false
		} else {
			return true
		}
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) {
			if(alerttxt!='')
			alert(alerttxt);
			return false
		} else {
			return true
		}
	}
}

function validate_pwd(field) {
	with (field) {
		if (field.value.match(/ +/)) {
			alert("Spaces not allowed in Password.");
			return false;
		} else if ((field.value.replace(/^\s*|\s*$/g,'').length <6)||(field.value.replace(/^\s*|\s*$/g,'').length >20)) {
			alert("Password should be of 6-20 characters (Spl. characters ! @ # & _ are allowed)");
			return false;
		} else if (!field.value.match(/^[0-9a-zA-Z!@#&_]+$/i)) {
			alert("Password should be of 6-20 characters (Spl. characters ! @ # & _ are allowed)");
			return false;
		}
		else {
			return true;
		}
	}
}

function valbutton(field,alerttxt) {
	with (field) {
		// place any other field validations that you require here
		// validate myradiobuttons
		myOption = -1;
		for (i=field.length-1; i > -1; i--) {
			if (field[i].checked) {
				myOption = i;
				i = -1;
			}
		}
		if (myOption == -1) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function vaidateSpecialChar(field,alerttxt) {
   with (field) {
	 var iChars = "!@#$%^&*()+=[]\\\';,./{}|\":<>?~ "; 
	   for (var i = 0; i < field.value.length; i++) {
		if (iChars.indexOf(field.value.charAt(i)) != -1) {
		  alert (alerttxt);
		  return false;
		}
	  }
	  return true;
   }
}

function validate_mobileNo(field,alerttxt) {
	with (field) {
		if(!isNaN(field) || field.value.length != 10) {
			if(alerttxt!='')
			alert(alerttxt);
			field.focus();
			return false;
		} else {
			return true;
		}
	}
}
/*var digitsInIPhoneNumber = 10;

function isInteger(s) {   
    var i;
	alert(s.length);
    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 checkPhone(strPhone){
	alert(strPhone.length);
	return (isInteger(strPhone) && (strPhone.length==digitsInIPhoneNumber));
}*/

function validateDob(field) {
	var currentTime = new Date();  // value fatch by system
	//var hours = currentTime.getHours()
	//var minutes = currentTime.getMinutes()
	var yearBySystem = currentTime.getFullYear();
	var fixyear= yearBySystem-'13'; //under 13 year old user cunt't able to insert the date of birth

	var splityear= field.value;  ///value fatech by text box
	splityear=splityear.split("/"); 
	yearByTextBox=splityear[2];	
	var FinalYear=yearBySystem-yearByTextBox;
	if(FinalYear <=12) {
		alert("Please select your Birth Date before "+fixyear+" year!");
		//document.forms['verificationFrm'].b_date.value="";
		return false;
	} else {
		return true;
	}
}
		

function valid_limiter(field,maxChar) {
	var textlength=field.value;  
	var enter = /\n/gi; //Value of Enter
	var textlength = textlength.replace(enter,"\r\n"); 
	var textlengthnew=textlength.length;	
	if (textlengthnew > maxChar)  { 
		alert("We only allow maximum of "+maxChar+" characters per post.\nIf your post is longer than "+maxChar+" characters then you can split it up into multiple messages.");
		field.focus();
		return false;
	} else if(field.value.replace(/^\s*|\s*$/g,'') =="" || field.value==field.defaultValue) {
		alert("Please enter text in text area.");
		field.value ='';
		field.focus();
		return false;
	} else {
		return true;
	}
}

function validateNewPost(thisform,maxChar) {	
	with (thisform) {
		 if(!valid_limiter(postValue,maxChar)) {
			 return false;
		 } else {
			// For Send SMS
			if(postSms.checked) {
				postSms.value = 1;
			} else {
				postSms.value = 0;
			}
			thisform.submit();
		}
	}
}

/** Home Page function **/
function confirmPostDel() {
		var del_post = confirm("Are you sure to delete this Post?")
		if(del_post == true)
		return true;
		else
		return false;
}


function validateComment(thisform,maxChar) {	
	with (thisform) {
		 if(!valid_limiter(vchCommentDesc,maxChar)) {
			 return false;
		 } else {
			thisform.submit();
		}
	}
}

function validateMoblieNo(thisform) {
	with (thisform) {
		if (validate_required(bintUserCellNoNew,"Please Enter New Mobile No")==false) {
			bintUserCellNoNew.focus();
			return false;
		} else if(validate_mobileNo(bintUserCellNoNew,"Invalid New Mobile No")==false) {
			return false;
		} else if (validate_required(bintUserCellNoVer,"Please Re Enter New Mobile No")==false) {
			bintUserCellNoVer.focus();
			return false;
		} else if(validate_mobileNo(bintUserCellNoVer,"Invalid Re New Mobile No")==false) {			
			return false;
		} else if (bintUserCellNoNew.value != bintUserCellNoVer.value) {
			alert("Re Enter Mobile No Does Not Match with New Mobile No");
			bintUserCellNoVer.focus();
			return false;
		} else {
			thisform.submit();
		    return true;
		}
	}
}

function validateVerifyCell(thisform) {
	with (thisform) {
		if (validate_required(vchUserCellVerifyCode,"Please Enter VerifyCode")==false) {
			vchUserCellVerifyCode.focus();
			return false;
		} else {
			thisform.submit();
		    return true;
		}
	}
}

function validateEmail(thisform) {
	with (thisform) {
		if (validate_required(vchUserEmailNew,"Please Enter New Email Id")==false) {
			vchUserEmailNew.focus();
			return false;
		} else if (validate_email(vchUserEmailNew,"Invalid New Email Id")==false) {
			vchUserEmailNew.focus();
			return false;
		} else if (validate_required(vchUserEmailVer,"Please Re Enter New Email Id")==false) {
			vchUserEmailVer.focus();
			return false;
		} else if (validate_email(vchUserEmailVer,"Invalid Re Email Id")==false) {
			vchUserEmailVer.focus();
			return false;
		} else if (vchUserEmailNew.value != vchUserEmailVer.value) {
			alert("Re Enter Email Id Does Not Match with New Email Id");
			vchUserEmailVer.focus();
			return false;
		} else {
			thisform.submit();
		    return true;
		}
	}
}

function validatePwd(thisform) {
	with (thisform) {
		if (validate_required(vchUserPwd,"Please Enter current Password")==false) {
			vchUserPwd.focus();
			return false;
		} else if (validate_required(vchUserPwdNew,"Please Enter New Password")==false) {
			vchUserPwdNew.focus();
			return false;
		} else if (validate_pwd(vchUserPwdNew)==false) {
			vchUserPwdNew.focus();
			return false;
		} else if (validate_required(vchUserPwdVer,"Please Re Enter New Password")==false) {
			vchUserPwdVer.focus();
			return false;
		} else if (validate_pwd(vchUserPwdVer)==false) {
			vchUserPwdVer.focus();
			return false;
		} else if (vchUserPwdNew.value != vchUserPwdVer.value) {
			alert("Re Enter Password Does Not Match with New Password");
			vchUserPwdVer.focus();
			return false;
		} else {
			thisform.submit();
		    return true;
		}
	}
}

function validate_photoExt(field,imageExt) {
	with (field) {
		var i = field.value.lastIndexOf('.');
		var l = field.value.length -i;
		var ext = field.value.substr(i+1,l);
		if(ext == 'JPG' || ext == 'jpg'|| ext == 'gif' || ext == 'png' || ext == 'jpeg'){
			imageExt.value = ext;
			return true;
		} else {
			alert("Please Upload jpg or gif or png or jpeg image only.");
			return false;
		}
	}
}


function validatePhoto(thisform) {
	with (thisform) {
		if (validate_required(vchGrpImageNameNew,"Images is not Uploaded, Please upload it.")==false) {
			vchGrpImageNameNew.focus();
			return false;
		} else if (validate_photoExt(vchGrpImageNameNew,imageExt)==false) {	 
			vchGrpImageNameNew.focus();
			return false;
		} else {
			thisform.submit();
		    return true;
		}
	}
}

// For msg length limiter
function limiter(thistext,count) {
	 var txtVal = thistext.value;
	 //txtVal =txtVal.replace(/\r/g,"");
	 var enter	=	/\n/gi; //Value of Enter
	 var txtVal	=	txtVal.replace(enter,"\r\n"); 		 
	 var txtLen =txtVal.length;
	 if(txtLen > count) {
		 txtVal=txtVal.substring(0,count);
		 thistext.value=txtVal;
		 return false;
	 } else	{
		  document.getElementById('ct1').innerHTML = count- txtLen ;
	 }		
}

function limiter2(thistext,count) {
	 var txtVal = thistext.value;
	 //txtVal =txtVal.replace(/\r/g,"");
	 var enter	=	/\n/gi; //Value of Enter
	 var txtVal	=	txtVal.replace(enter,"\r\n"); 		 
	 var txtLen =txtVal.length;
	 if(txtLen > count) {
		 txtVal=txtVal.substring(0,count);
		 thistext.value=txtVal;
		 return false;
	 } else	{
		  document.getElementById('ct2').innerHTML = count- txtLen ;
	 }		
}

