function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


function IsDigit(o,e) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (((code < 48)||(code > 57 ))) 
	{
		//if (code != 46)
		//{
			return false;
		//}
	}
}
function IsDigitDecimal(o,e) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (((code < 48)||(code > 57 ))) 
	{
		if (code != 46)
		{
			return false;
		}
	}
}
function checkDigit(objParam)
{
	var str = document.getElementById(objParam).value;
	var str1 = "";
	for (i = 0 ;i <= str.length ;i++ )
	{
		for (j = 0; j < 10 ;j++ )
		{
			if (str.charAt(i) == j)
			{
				str1 = str1 + str.charAt(i);
			}
		}
	}
	document.getElementById(objParam).value = str1;
	
}
function checkDigitWithDecimal(objParam)
{
	var str = document.getElementById(objParam).value;
	var str1 = "";
	for (i = 0 ;i <= str.length ;i++ )
	{
		for (j = 0; j < 10 ;j++ )
		{
			if (str.charAt(i) == j)
			{
				str1 = str1 + str.charAt(i);
			}
		}
		if (str.charAt(i) == "." && str1.indexOf(".") < 0)
		{
			str1 = str1 + str.charAt(i);
		}
	}
	document.getElementById(objParam).value = str1;
	
}

function stripFirstZero(objParam){
	var str = document.getElementById(objParam).value;
	
	while (str.charAt(0) == '0' && str != '0')
	{
		str = str.substring(1, str.length);
	}
	document.getElementById(objParam).value = str;
}
function Trim(inputString)
{
	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == " ")
	{ // Check for space at the start of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}

	ch = retValue.substring(retValue.length-1, retValue.length);

	while (ch == " ")
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}	
	return retValue;
}
function popUp(URL, w, h, t, l) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+w+",height="+h+",top="+t+",left="+l+"');");
}
function IsEmpty(v){
	varValue = Trim(v);
	if (varValue.length == 0){ return true; }else{ return false; }
}
function IsValidEmail(str){
	var at = '@';
	var dot = '.';
	var lat = parseInt(str.indexOf(at));
	var ldot = parseInt(str.lastIndexOf(dot));
	var lstr = parseInt(str.length);
	
	//no '@' or '@' is first character or '@' is the last character
	if ((lat <= 0) || (lat == parseInt(lstr-1)))
		return false;

	//no '.' or '.' is first character or '.' is the last character
	if ((ldot <= 0) || (ldot == parseInt(lstr-1)))
		return false;

	//presence of another '@'
	if (str.indexOf(at, parseInt(lat+1)) != -1) 
		return false;

	//presence of '.' before or after '@'
	if ((str.substr(parseInt(lat - 1), 1) == dot) || (str.substr(parseInt(lat + 1), 1) == dot))
		return false;

	//check '.' is at least one character after '@'
	if (str.indexOf(dot, parseInt(lat + 2)) == -1) 
		return false;

	//check for blank
	if (str.indexOf(" ") != -1) 
		return false;

	//check the length after the last '.' is not less than 2 characters
	if (str.substr(parseInt(ldot + 1)).length < 2) 
		return false;

	if (!IsAlphaNumeric(str.substr(ldot + 1)))
		return false;	
	
	return true;
}
function IsValidPassword(v){
	if (IsAlphaNumeric(v)) {
		if (v.length < 6) { return false; } else { return true; }
	} else { return false; }
}
function IsAlphaNumeric(str){
	for (i=0; i<str.length; i++){
		if (!((str.charCodeAt(i)>=97) && (str.charCodeAt(i)<=122)) && !((str.charCodeAt(i)>=65) && (str.charCodeAt(i)<=90)) && !((str.charCodeAt(i)>=48) && (str.charCodeAt(i)<=57))){
			return false;
		}
	}
	return true;
}

function clearText(objParam){
	document.getElementById(objParam).value = "";
}

function validate(){
	if (IsEmpty(document.getElementById("name").value))
	{
		alert("Please enter your name");
		document.getElementById("name").focus();
		return false;
	}
	if (IsEmpty(document.getElementById("email").value))
	{
		alert("Please enter your email");
		document.getElementById("email").focus();
		return false;
	}
	else
	{
		if (!IsValidEmail(document.getElementById("email").value))
		{
			alert("Please enter a valid email");
			document.getElementById("email").focus();
			return false;
		}
	}
	if (IsEmpty(document.getElementById("enquiry").value))
	{
		alert("Please enter your enquiry");
		document.getElementById("enquiry").focus();
		return false;
	}
	document.form.action = "contact_action.php";
	document.form.submit();
}

function validate_career(){
	if (IsEmpty(document.getElementById("enquirytype").value))
	{
		alert("Please select your application type.");
		document.getElementById("enquirytype").focus();
		return false;
	}
	if (IsEmpty(document.getElementById("name").value))
	{
		alert("Please enter your name");
		document.getElementById("name").focus();
		return false;
	}
	if (IsEmpty(document.getElementById("email").value))
	{
		alert("Please enter your email");
		document.getElementById("email").focus();
		return false;
	}
	else
	{
		if (!IsValidEmail(document.getElementById("email").value))
		{
			alert("Please enter a valid email");
			document.getElementById("email").focus();
			return false;
		}
	}
	if (IsEmpty(document.getElementById("enquiry").value))
	{
		alert("Please enter your Self PR.");
		document.getElementById("enquiry").focus();
		return false;
	}
	if (IsEmpty(document.getElementById("file").value))
	{
		alert("Please upload your resume.");
		document.getElementById("file").focus();
		return false;
	}
	document.form.action = "career_app_action.php";
	document.form.submit();
}