﻿//'-----------------------------------------------------------------------------------'----------------------
//'Copyright:
//'This code is the property of NetEdge Computing Solutions Private Limited.
//'No one is allowed to use it without the written permission from NetEdge. 
//'----------------------------------------------------------------------------------'-----------------------

/********************************************GENERAL FUNCTIONS*****************************************************/
var ControlPrefix = "ctl00_ContentPlaceHolder1_";

function Invalid(fld, extra_char) 
{
	var NUMBERS = "0123456789";
	var CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	if(fld == "") 
	{ 
		return false; 
	}	
	
	switch(extra_char.toLowerCase())
	{
		case 'numbers' :
		{
			extra_char = NUMBERS;
			break;
		}
		case 'title' :
		{
			extra_char = " '()" + CHARS;
			break;
		}		
		case 'name' :
		{
			extra_char = "" + CHARS;
			break;
		}
		case 'fullname' :
		{
			extra_char = " " + CHARS;
			break;
		}
		case 'login' :
		{
			extra_char = "_-@." + NUMBERS + CHARS;
			break;
		}
		case 'password' :
		{
			extra_char = "@-._" + NUMBERS + CHARS;
			break;
		}
		case 'address' : 
		{		
			extra_char = " -~@#&*()-,_=+{}]['\n/ " + NUMBERS + CHARS;
			break;
		}
		case 'desc' : 
		{
			extra_char = ".\n\r\t,~!@#$%^&*()_+=-`[]}{\|''/\\/ " + NUMBERS + CHARS;
			break;
		}
		case 'city_state_country' : 
		{
			extra_char = " " + CHARS;
			break;
		}
		case 'zip' : 
		{
			extra_char = "-" + NUMBERS + CHARS;
			break;
		}
		case 'phone_mobile_fax' : 
		{
			extra_char = "+- ()," + NUMBERS;
			break;
		}
		case 'subject' :
		{
			extra_char = "http://www.&@$!~-_+{}:<>?*^'\"-.\n\r\t,~%^&*()=`[]\|''/\\/" + NUMBERS + CHARS;
			break;
		}
		case 'file' :
		{
			extra_char = ":\\._- " + NUMBERS + CHARS;
			break;
		}		
		case 'job_company' :
		{
			extra_char = "-'()@*./," + NUMBERS + CHARS;
			break;
		}
		case 'education' :
		{
			extra_char = ", -;:.'()[]{}" + NUMBERS + CHARS;
			break;
		}
		case 'url' : 
		{
			extra_char = "http://www.&@$!~-_+{}:<>?*^=" + NUMBERS + CHARS;
			break;
		}	
		case 'designation' : 
		
		{
			extra_char = ",&@()/.-' " + NUMBERS + CHARS;
			break;
		}	
		case 'js_variable_friendly_for_doublequote' : 
		{
			extra_char = "http://www.&@$!~-_+{}:<>?*^.,~!@#$%^&*()_+=-`[]}{\|'/ " + NUMBERS + CHARS;
			break;
		}	
		case 'js_variable_friendly_for_singlequote' : 
		{
			extra_char = "http://www.&@$!~-_+{}:<>?*^.,~!@#$%^&*()_+=-`[]}{\|/ " + NUMBERS + CHARS;
			break;
		}	
		case 'general_title' : 
		{
			extra_char = "\"http://www.&@$!~-_+{}:<>?*^.,~!@#$%^&*()_+=-`[]}{\|/\' " + NUMBERS + CHARS;
			break;
		}	
		case 'job_title' : 
		{
			extra_char = ".@$-_,()`/\"' " + NUMBERS + CHARS;
			break;
		}
		case 'currency' : 
		{
			extra_char = ".$' " + NUMBERS + CHARS;
			break;
		}
		case 'alphanumeric_space' : 
		{
			extra_char = " " + NUMBERS + CHARS;
			break;
		}
		case 'alphanumeric' : 
		{
			extra_char = NUMBERS + CHARS;
			break;
		}
		default :
		{
			extra_char = "http://www.&@$!~-_+{}:<>?*^.\n\r\t,~!@#$%^&*()_+=-`[]}{\|''/\\/ " + NUMBERS + CHARS;
		}
	}	
	
	for (var i = 0; i < fld.length; i++) 
	{ 
		if (extra_char.indexOf(fld.charAt(i)) == -1)
		{
			return false;	
		}
	}   	
	return true
}

function ValidateEmailID(arg_emailid_obj)
{
// First Trim the extra spaces 

	var emailid = "";	
	
	if(arg_emailid_obj != null)
	{
		emailid = trim(arg_emailid_obj.value);
	}

	if (emailid == '') 
	{
		alert ("Please enter Email ID");
		arg_emailid_obj.focus();	
		return false;
	}
	
// check for valid cheracters first
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < emailid.length; i++) 
	{
		var letter = emailid.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
		continue;
		parsed = false;
		break;
	}
  // if any invalid cheracter is there then exit 
	if (parsed==false) 
	{
	alert("Your email address contains invalid characters.");
	return false;
	}
	// check if email id is null
	if (emailid==null)
	{
		alert("Please enter your Email Id to proceed");
		return false;
	}
	// check for the occurance of @ cheracter

	if(emailid.indexOf('@')==-1 )
	{
		alert ("Email Id must contain @");
		return false;
	}

	// check for the multiple occurance of @ cheracter

	if(emailid.indexOf('@')!=emailid.lastIndexOf('@') )
	{
		alert ("Email Id must not contain more than one @ symbol");
		return false;
	}

	// check for the occurance of the . character
	 if(emailid.indexOf('.')==-1 ) 
	{
		alert ("Email Id must contain dot(.)");
		return false;
	}
	
	// check if @ is occuring in the begining 
	if (emailid.indexOf('@')==0 || emailid.indexOf('.')==0)
	{
		alert ("Email Id must not contain @ or dot(.) in the beginning");
		return false;
	}


	// check for consicutive dot(.)
	for (i=0;i<(emailid.length-1) ; i++ )
	{
		if ((emailid.charAt(i)=='.' && emailid.charAt(i+1)=='.')) 
		{
			alert ("Email Id must not contain consecutive dots(.)");
			return false;
		}
	}

	// check if . is occuring at the end of the email id

	var a1=emailid.lastIndexOf('.');
	var a2=emailid.length;
	a2--;	
	if(a1==a2)
	{
		alert ("Email Id must contain a valid domain name");
		return false;
	}

	// check if @ is occuring at the end of email id
	 a1=emailid.indexOf('@');
	 a2=emailid.length;
	a2--;	
	if(a1==a2)
	{
		alert ("Email Id must not contain @ at the end");
		return false;
	}


	// check for characters between @ and dot(.)
	for (i=0;i<(emailid.length-1) ; i++ )
	{
		if ((emailid.charAt(i)=='@' && emailid.charAt(i+1)=='.') || (emailid.charAt(i)=='.' && emailid.charAt(i+1)=='@')) 
		{
			alert ("Email Id must contain some character between @ and dot(.)");
			return false;
		}
	}

	// check if dot is occuring after @ or not
	a1=emailid.indexOf('@');
	a2=emailid.lastIndexOf('.');
	if (a1> a2)
	{
		alert ("Email Id must contain a dot(.) after @ ");
		return false;
	}
		
	return true;
}
/*************************************************************************************************************************/
/********************************************END OF GENERAL FUNCTIONS*****************************************************/

function ProcessChangePassword()
{				
	var CurrentPassword = document.getElementById("txtCurrentPassword").value;
	if (CurrentPassword == "")
	{
		alert('Current Password cannot be blank');
		document.getElementById("txtCurrentPassword").focus();
		return false;
	}

	var NewPassword = document.getElementById("txtNewPassword").value;
	if (NewPassword == "")
	{
		alert('New Password cannot be blank');
		document.getElementById("txtNewPassword").focus();
		return false;
	}

	var ConfirmPassword = document.getElementById("txtConfirmPassword").value;
	if (ConfirmPassword == "")
	{
		alert('Confirm Password cannot be blank');
		document.getElementById("txtConfirmPassword").focus();
		return false;
	}
	
	if (NewPassword != ConfirmPassword)
	{
		alert('New Password and Confirm Password are not same');
		document.getElementById("txtConfirmPassword").focus();
		return false;	
	}

	if(confirm("Do you really want to change your password?") == true)
		return true;
	else
		return false;	
} 

//function for trim 
function trim(str)
{
	if(str == "" || str == null)
		return str;

	while (str.charAt(0) == " ")
	{
	// remove leading spaces
		str = str.substring(1);
	}
	while (str.charAt(str.length - 1) == " ")
	{
	// remove trailing spaces
		str = str.substring(0,str.length - 1);
	}

	return str;
}
 
function Close()
{
	window.close();
}

/**************************************FILE FIELD VALIDATION STARTS HERE ********************************************************/
function ValidateFileField(file_object)
{
	var filename = file_object.value.toUpperCase();
	 
	if(filename.lastIndexOf('.RTF') == "-1" && filename.lastIndexOf('.DOC')=="-1" && filename.lastIndexOf('.PDF')=="-1" && filename.lastIndexOf('.HTM')=="-1" && filename.lastIndexOf('.HTML')=="-1")
	{
		alert("Only file with the extension of '.RTF / .DOC / .PDF / .HTM / .HTML' are allowed");
		file_object.value = "";
		file_object.focus();
		return false;
	}
	else 
		return true;
}
function ValidateImageField(file_object)
{
	var filename = file_object.value.toUpperCase();
	 
	if(filename.lastIndexOf('.GIF') == "-1" && filename.lastIndexOf('.JPG')=="-1" && filename.lastIndexOf('.JPEG')=="-1" && filename.lastIndexOf('.PNG')=="-1")
	{
		alert("Only file with the extension of '.GIF / .JPG / .JPEG / .PNG are allowed");
		file_object.value = "";
		file_object.focus();
		return false;
	}
	else 
		return true;
}
/**************************************DATE VALIDATION STARTS HERE ********************************************************/
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    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 stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	/*month=parseInt(strMonth)
	day=parseInt(strDay)
	*/
	month=parseInt(strDay)
	day=parseInt(strMonth)
	
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	return true;
}

function ValidateFileAndURL(file_url, fieldObj)
{
	if(file_url == "file")
	{
		var filename = fieldObj.value.toUpperCase();
		
		if(ValidateFileField(fieldObj) == false)
			return false;
	}
	else if(file_url == "url")
	{	
		if(fieldObj.value.indexOf("http://") == "-1" || fieldObj.value.indexOf(".") == "-1" || Invalid(fieldObj.value, "url") == false || fieldObj.value == "http://" || fieldObj.value == "http://www" || fieldObj.value == "http://www.")		
		{
			alert("URL must be of the format 'http://www.<domain name>.<org/net/com/etc>'");
			//fieldObj.value = "";
			fieldObj.focus(); 
			return false;
		}		
	}
	else
		return true;
}

/********************************DATE VALIDATION ENDS HERE**********************************************/
