// validation code for form fields
var alphaNumExp = /^[0-9a-zA-Z_.&'"-, ]+$/;
var alphaNumSpCharExp = /^[0-9a-zA-Z_,.-@&\/\:; ]+$/;
var passwordExp = /^[0-9a-zA-Z_]+$/;
var alphaExp = /^[0-9a-zA-Z_,. ]+$/;
var onlyAlphaExp = /^[a-zA-Z_ ]+$/;
var numericExp = /^[0-9-+() ]+$/;
var onlyNumericExp = /^[0-9]+$/;
var emailExp = /^[a-zA-Z0-9.@_ ]+$/;

function validateFields(str, validateExp, strlen, mandatory, obj, title){
	var returnValue = 0;
	var pos;
	var ext;
//	var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	

	if((title == 'Tele Number') || (title == 'Tele Number 1')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain alphabets and special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	}else if((title == 'Tele Number 2') || (title == 'Tele Number 3') || (title == 'Tele Number 4') || (title == 'Fax Number') || (title == 'Fax Number 1') || (title == 'Fax Number 2') || (title == 'Fax Number 3') || (title == 'Fax Number 4') || (title == 'Mobile Number') || (title == 'Mobile Number 1') || (title == 'Mobile Number 2') || (title == 'Mobile Number 3') || (title == 'Mobile Number 4')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain alphabets and special characters such as ~!@#$%^&*()";
	}else if((title == 'Company Profile') || (title == 'Product Description')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain special characters such as ~!@#$%^&*()";
	}else if((title == 'Company Name') || (title == 'Name') || (title == 'Product Name') || (title == 'Applications') || (title == 'Keywords')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	}else if((title == 'Feedback') || (title == 'Requirements') || (title == 'Comments')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot be blank.";
	}else{
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain numbers and special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	}
	
	if(mandatory == 'Y'){
		if(str == '') returnValue = 1;
		else{
			if(validateExp != null){
				if(!(str.match(validateExp)))	returnValue = 1;
			}
		}
	}else{
		if(validateExp != null){
			if(str != ''){
				if(!(str.match(validateExp))) returnValue = 1;
			}
		}
	}
	if(returnValue == 1){
		alert(msg);
		if(document.getElementById(''+obj+''))
			document.getElementById(''+obj+'').focus();
		return false;
	}else return true;
}

function validatePasswords(cpwd, pwd){
	if(cpwd != pwd){
		alert('The New and Confirm Passwords do not match.');
		document.getElementById('newpassword').focus();
		return false;
	}else return true;
}

function emailCheck (emailStr) {

	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
 		alert("Email address seems incorrect (check @ and .'s)");
		if(document.getElementById('email'))
			document.getElementById('email').focus();
		return false;
	}

	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
		alert("The Email doesn't seem to be valid.");
		if(document.getElementById('email'))
			document.getElementById('email').focus();
    	return false;
	}

	var IPArray=domain.match(ipDomainPat)

	if (IPArray!=null) {
   	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("The Email doesn't seem to be valid.");
			if(document.getElementById('email'))
				document.getElementById('email').focus();
			return false;
	    }
      }
      return true;
	}

	var domainArray=domain.match(domainPat)

	if (domainArray==null) {
		alert("The Email doesn't seem to be valid.");
		if(document.getElementById('email'))
			document.getElementById('email').focus();
	    return false;
	}

	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
	   alert("The Email doesn't seem to be valid.");
		if(document.getElementById('email'))
			document.getElementById('email').focus();
	   return false;
	}

	if (len<2) {
	   alert("The Email doesn't seem to be valid.");
		if(document.getElementById('email'))
			document.getElementById('email').focus();
	   return false;
	}
	return true;
}


function websiteCheck(website, obj){
	if(website != ''){
		var url = website;
		var pos = 0;
		pos = url.indexOf(".");
		if((url.substr(0,pos)) != 'www'){					// validates whether url starts with www
			alert("Entered Website URL is invalid");
//			document.getElementById(''+obj+'').focus();
			return false;
		}
		var website1 = url.substr((pos+1),(url.length));
		var pos1 = website1.indexOf(".");
		var len = website1.length;
		var domain = website1.substr((pos1+1),len);
		if((domain != 'com') && (domain != 'org') && (domain != 'co.uk') && (domain != 'gov') && (domain != 'edu') && (domain != 'net') && (domain != 'co.in')){
			alert("Entered Company URL is invalid");
//			document.getElementById(''+obj+'').focus();
			return false;
		}	
		if(website.length > 50){
			alert("Entered Company URL is invalid !.\n\n * URL cannot exceed 50 characters.\n * URL cannot contain special characters such as ~!#$%^&*().");
//			document.getElementById(''+obj+'').focus();
			return false;
		}
		if(!(website.match(alphaNumExp))){
			alert("Entered Company URL is invalid !.\n\n * URL cannot exceed 50 characters.\n * URL cannot contain special characters such as ~!#$%^&*().");
//			document.getElementById(''+obj+'').focus();
			return false;
		}
	}/*else{
		alert("Please enter Company URL");
//		document.getElementById(''+obj+'').focus();
		return false;
	}	*/
	return true;
}

function validateImage(imgName){
	var pos = imgName.indexOf('.', 0);
	var ext = imgName.substr((pos+1), (imgName.length));
	if((ext != 'jpg') && (ext != 'JPG') && (ext != 'gif') && (ext != 'GIF') && (ext != 'pdf') && (ext != 'PDF') && (ext != 'doc') && (ext != 'DOC') && (ext != 'docx') && (ext != 'DOCX') && (ext != 'ppt') && (ext != 'PPT') && (ext != 'pptx') && (ext != 'PPTX')){
		alert("Invalid Company Photo / Profile File / Catalogue Files. Allowed file extensions are 'jpg', 'gif', 'pdf', 'doc', 'docx'");
		return false;
	}
	return true;
}

function validateCompanyImage(imgName){
	var pos = imgName.indexOf('.', 0);
	var ext = imgName.substr((pos+1), (imgName.length));
	if((ext != 'jpg') && (ext != 'JPG') && (ext != 'gif') && (ext != 'GIF')){
		alert("Invalid Company Photo. Allowed file extensions are 'jpg', 'gif'");
		return false;
	}
	return true;
}

function validateProductImage(imgName){
	var pos = imgName.indexOf('.', 0);
	var ext = imgName.substr((pos+1), (imgName.length));
	if((ext != 'jpg') && (ext != 'JPG') && (ext != 'gif') && (ext != 'GIF')){
		alert("Invalid Product Photo. Allowed file extensions are 'jpg', 'gif'");
		return false;
	}
	return true;
}

function validateFieldsTab(str, validateExp, strlen, mandatory, obj, title){
//	alert(str+validateExp+strlen+mandatory+obj+title);
	var returnValue = 0;
	var pos;
	var ext;
//	var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	

	if((title == 'Tele Number') || (title == 'Tele Number 1')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain alphabets and special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	}else if((title == 'Tele Number 2') || (title == 'Tele Number 3') || (title == 'Tele Number 4') || (title == 'Fax Number') || (title == 'Fax Number 1') || (title == 'Fax Number 2') || (title == 'Fax Number 3') || (title == 'Fax Number 4') || (title == 'Mobile Number') || (title == 'Mobile Number 1') || (title == 'Mobile Number 2') || (title == 'Mobile Number 3') || (title == 'Mobile Number 4')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain alphabets and special characters such as ~!@#$%^&*()";
	}else if(title == 'Company Profile'){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain special characters such as ~!@#$%^&*()";
	}else if((title == 'Company Name') || (title == 'Name')){
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	}else{
		var msg = "Entered " + title + " is invalid !.\n\n * " + title + " cannot exceed "+ strlen + " characters.\n * " + title + " cannot contain numbers and special characters such as ~!@#$%^&*()\n * " + title + " cannot be blank.";
	}
	
	
	if(mandatory == 'Y'){
		if(str == '') returnValue = 1;
		else{
			if(validateExp != null){
				if(!(str.match(validateExp)))	returnValue = 1;
			}
		}
	}else{
		if(validateExp != null){
			if(str != ''){
				if(!(str.match(validateExp))) returnValue = 1;
			}
		}
	}
	if(returnValue == 1){
		alert(msg);
		return false;
	}else return true;
}

