// JavaScript Document

function IsEmailValid(obj) {
	var emailId = obj.value;
	var apos=emailId.indexOf("@");
	var dotpos=emailId.lastIndexOf(".");
	var lastpos=emailId.length-1;

	if (emailId.indexOf(' ')==-1 && 0 < emailId.indexOf('@') && 0 < emailId.indexOf('.') && emailId.indexOf('@')+1 < emailId.length && emailId.length >= 5)
	{
		return true;
	}
	return false;
}

function searching(dspl) {
	document.getElementById("products1").style.display="none";
	document.getElementById("products2").style.display="none";
	
	document.getElementById("buyers1").style.display="none";
	document.getElementById("buyers2").style.display="none";
	
	document.getElementById("suppliers1").style.display="none";
	document.getElementById("suppliers2").style.display="none";
	
	document.getElementById("selling_leads1").style.display="none";
	document.getElementById("selling_leads2").style.display="none";
	
	document.getElementById("buying_leads1").style.display="none";
	document.getElementById("buying_leads2").style.display="none";
	
	document.getElementById(dspl+"1").style.display="block";
	document.getElementById(dspl+"2").style.display="block";
}

function popImg(iName) {
	var pURL=iName;
	pInfo='toolbar=0,';
	pInfo+='location=0,';
	pInfo+='directories=0,';	
	pInfo+='status=0,';
	pInfo+='menubar=0,';
	pInfo+='scrollbars=0,';
	pInfo+='resizable=0';
	window.open(pURL, 'Image', pInfo);
}

// Check if string is blank
function isBlank(str) {
    for (var i = 0; i < str.length; i++) {
        var chr = str.charAt(i);
        if ((chr != ' ') && (chr != '\n') && (chr != '\t')&& (chr != '\r')&& (chr != '\r\n')) {
            return false;
        }
    }
    return true;
}

function ShowPointer(obj) {
	obj.style.cursor = "pointer";
}

function changeBox()
{
    document.getElementById('passDiv1').style.display='none';
    document.getElementById('passDiv2').style.display='';
    document.getElementById('password').focus();
}

function restoreBox()
{
    if(document.getElementById('Password').value=='')
    {
      document.getElementById('passDiv1').style.display='';
      document.getElementById('passDiv2').style.display='none';
    }
}


function clearTextBox(obj, val, type) {
	if (obj.value == val) {
		obj.value = "";
		if (type == "pass")
		{
			obj.type="password";
		}
	}
	return true;
}

function populateTextBox(obj, val, type) {
	if (obj.value == "") {
		obj.value = val;
		if (type == "pass")
		{
			obj.type = "text";
		}
	}
	return true;
}

// Validate login details
function validateLogin(objEmail, objPass) {
	if (!valideEmail(objEmail.value)) {
		alert("Please enter a valid Email Address.");
		objEmail.focus();
		return false;
	}
	else if (objPass.value == "") {
		alert("Please enter Password.");
		return false;
	}
	return true;
}

// Validate email address
function valideEmail(emailVal) {
	if (emailVal=="") {
		return false;
	}
	else if (emailVal.indexOf('@',0) == -1 || emailVal.indexOf('.',0) == -1) {
		return false;
	}
	return true;
}

// Move cursor in textarea
function setSelRange(inputEl, selStart, selEnd) {  
	if (inputEl.setSelectionRange) {   
		inputEl.focus();   
		inputEl.setSelectionRange(selStart, selEnd);  
	} else if (inputEl.createTextRange) {   
		var range = inputEl.createTextRange();   
		range.collapse(true);   
		range.moveEnd('character', selEnd);   
		range.moveStart('character', selStart);   
		range.select();  
	} 
}

// Validate the URL is of http://www.mydomain.com format
function isValideURL(url) {
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");

    if (!v.test(url)) {
        return false;
    }
	return true;
}

// Confirm adding company to contacts list.
function addContact(id, contactsForm)
{
	var result = confirm("Add company to your contacts list?");

	if (result==true)
	{
		contactsForm.addContact.value = "true";
		contactsForm.contactID.value = id;
		contactsForm.submit();
	}
}

// Check if need to show ad dcompany to contacts list message
function onLoadCheck(param1, param2, companyName)
{
	if (param1 == "1")
	{
		if (param2)
		{
			alert(companyName + " has been added successfully to your 'Contacts List' available under the 'My Account' tab.");
		}
		else
		{
			alert("Technical problem occured. Please try again.");
		}
	}
	else if (param1 == "2")
	{
		alert(companyName + " already exists in your 'Contacts List'.");		
	}
}

function isValidFileName(fileName)
{
	var sarray = new Array();
	sarray = fileName.split('\\');
	the_name = sarray.length;

	var FName = sarray[the_name - 1];
	var isFilenameLegal = /^[a-zA-Z0-9_-]*.[a-zA-Z0-9]*$/.test(FName,"g");

	if (!isFilenameLegal)
	{
//		alert('INVALID FILENAME: ' + sarray[the_name - 1] );
		return false;
	}
	else
	{
//		alert('Valid filename');
		return true;
	}
}

function openNewWindow(winURL, h, w) 
{
	window.open(winURL,null,"height="+h+",width="+w+",status=0,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1");
} 
