// JavaScript Document


// Email Address Validation
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
		return false;
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;

	if (str.indexOf(at,(lat+1))!=-1)
		return false;

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;

	if (str.indexOf(dot,(lat+2))==-1)
		return false;
		
	if (str.indexOf(" ")!=-1)
		return false;

return true					
}

// Integer Check
function isInteger(s)
{   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

// Currency Check
function isMoney(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")) && ((c != ".") && (c != "," )))
			return false;
		if (c == ".")
			if (s.length > (i + 3))
				return false;
    }
    return true;
}

//open new window script for XHTML 1.0 Strict
function externalLinks() {
	if (!document.getElementsByTagName) 
		return; 
	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++) { 
   		var anchor = anchors[i]; 
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
     		anchor.target = "_blank"; 
 	} 
} 

window.onload = externalLinks;

//dina corporate website
function check_user_login() {
	if (document.login_frm.username.value == "")
	{
		alert("please enter your username");
		document.login_frm.username.focus();
		return false;
	}
	
	if (document.login_frm.password.value == "")
	{
		alert("please enter your password");
		document.login_frm.password.focus();
		return false;
	}
	
	return true;
}

function check_user_registration() {
	if (document.login_frm.txtFirstName.value == "")
	{
		alert("please enter your first name");
		document.login_frm.txtFirstName.focus();
		return false;
	}
	
	if (document.login_frm.txtLastName.value == "")
	{
		alert("please enter your last name");
		document.login_frm.txtLastName.focus();
		return false;
	}
	
	if (document.login_frm.txtCompanyName.value == "")
	{
		alert("please enter company name");
		document.login_frm.txtCompanyName.focus();
		return false;
	}
	
	if (document.login_frm.txtUniform.value == "")
	{
		alert("please enter compulsory uniform");
		document.login_frm.txtUniform.focus();
		return false;
	}
	
	if (document.login_frm.txtEmail.value == "")
	{
		alert("please enter your valid email id");
		document.login_frm.txtEmail.focus();
		return false;
	}
	else if (!isEmailValid(document.login_frm.txtEmail.value))
	{
		alert("please enter your valid email id");
		document.login_frm.txtEmail.focus();
		return false;
	}
	
	if (document.login_frm.txtPassword.value == "")
	{
		alert("please enter password");
		document.login_frm.txtPassword.focus();
		return false;
	}
	else if (document.login_frm.txtPassword.value != document.login_frm.txtConfirmPassword.value)
	{
		alert("Password doesn't match.");
		document.login_frm.txtPassword.focus();
		return false;
	}
	
	return true;
}

function check_contactus()
{
	if (document.contact_us.txtname.value == "")
	{
		alert("please enter your name");
		document.contact_us.txtname.focus();
		return false;
	}
	
		
	if (document.contact_us.txtcompany.value == "")
	{
		alert("please enter company name");
		document.contact_us.txtcompany.focus();
		return false;
	}
	
	if (document.contact_us.txtemail.value == "")
	{
		alert("please enter your valid email id");
		document.contact_us.txtemail.focus();
		return false;
	}
	else if (!isEmailValid(document.contact_us.txtemail.value))
	{
		alert("please enter your valid email id");
		document.contact_us.txtemail.focus();
		return false;
	}
	
	if (document.contact_us.txtmessage.value == "")
	{
		alert("please enter your message.");
		document.contact_us.txtmessage.focus();
		return false;
	}
	
	return true;
}

function isEmailValid(sEmail) 
{
	if(sEmail.value == '' || sEmail.indexOf('.') == 0 || sEmail.indexOf('.') == -1 || sEmail.indexOf('@') == 0 || sEmail.indexOf('@') == -1 || sEmail.indexOf('.') == sEmail.length - 1 || sEmail.indexOf(',') >= 0) 
	return false;	
	else 

	return true;
}
	
function check_forgotten_password()
{
	if (document.login_frm.username.value == "")
	{
		alert("please enter your username.");
		document.login_frm.username.focus();
		return false;
	}
	else
	{
		if(!echeck(document.login_frm.username.value))
		{
			alert("please enter a valid username.");
			document.login_frm.username.focus();
			return false;
		}
	}
	
	return true;
}

function fnPopWin(vSrc, vwidth, vheight)
{
	if (vSrc != "")
	{
		var win;
		win = window.open('',"categories","height=" + (vheight+20) + ",width=" + (vwidth+20) + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no");
		win.document.open();
		win.document.write('<html><head><title>Image</title></head>' + '<body onLoad="window.focus();"><table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="middle"><table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="' + vSrc + '"></td></tr></table></td></tr></table></body></html>');
		win.document.close();
	}
}
