function Mandatory(myfield,errmsg,errname)
{
	if (myfield.value.length == 0 || myfield.value == "" || myfield.value == "0")
	{	
		document.getElementById(errname).innerHTML=errmsg;
		myfield.focus();
		return false;
	}
	document.getElementById(errname).innerHTML="";	
	return true;
}


function MandatoryRadio(myfield,errmsg,errname)
{
	var field = GetRadioValue(myfield);
	if (field.length == 0 || field == "" || field == "0")
	{
		document.getElementById(errname).innerHTML=errmsg;
		return false;
	}
	document.getElementById(errname).innerHTML="";	
	return true;
}


function CheckNumber(checkStr,errmsg,errname)
{
	var allValid = true;
	var string = checkStr.value;
	string = delete_min(string);
	string = delete_space(string);

	if(isNaN(string))
	{
		allValid = false;
		document.getElementById(errname).innerHTML=errmsg;
		checkStr.focus();
		checkStr.select();
	}
	return allValid;
}


function CharacterFilter1(checkOK,checkStr,errmsg,errname)
{
	var allValid = true;
	var flag = checkStr.value.indexOf(checkOK);
	if(flag==-1)
	{	
		allValid = false;
		document.getElementById(errname).innerHTML=errmsg;
		checkStr.focus();
		checkStr.select();
	}
	return allValid;
}



function CheckMandatory(myfield,errmsg)
{
	if (myfield.value.length == 0 || myfield.value == "" || myfield.value == "0")
	{
		alert(errmsg);
		myfield.focus();
		return false;
	}
	return true;
}



function CheckMandatoryCompare(myfield,errmsg,compare)
{
	if (myfield.value.length == 0 || myfield.value == "" || myfield.value == "0" || myfield.value == compare)
	{
		alert(errmsg);
		myfield.focus();
		return false;
	}
	return true;
}



function CheckCheckNumber(checkStr,errmsg)
{
	var allValid = true;
	var string = checkStr.value;
	string = delete_min(string);
	string = delete_space(string);
	
	if(isNaN(string))
	{
		allValid = false;
		alert(errmsg);
		checkStr.focus();
		checkStr.select();
	}
	return allValid;
}




function CheckCharacterFilter1(checkOK,checkStr,errmsg)
{
	var allValid = true;
	var flag = checkStr.value.indexOf(checkOK);
	if(flag==-1)
	{	
		allValid = false;
		alert(errmsg);
		checkStr.focus();
		checkStr.select();
	}
	return allValid;
}



function checking_remove(element_id)
{
	document.getElementById(element_id).value = "";
}


function checking_fill(element_id,fieldtext)
{
	if(document.getElementById(element_id).value=="")
	{
		document.getElementById(element_id).value = fieldtext;
	}
}



function GetRadioValue(radioObj) {
	if(!radioObj)
		return false;
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return false;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return false;
}

function delete_min(string)
{
		for(var i=0;1;i++)
		{
			var char=string.indexOf("-");
			if(char>0)
				{
					string = string.replace("-","");
				}
			else if(char<0)
				{
					break;
				}
		}
	
	return string;
}

function delete_space(string)
{
		for(var i=0;1;i++)
		{
			var char=string.indexOf(" ");
			if(char>0)
				{
					string = string.replace(" ","");
				}
			else if(char<0)
				{
					break;
				}
		}
		return string;
}

function checkfield1(myfield,message,compare)/* with comparation */
{	if (myfield.value.length == 0 || myfield.value == "" || myfield.value == "0" || myfield.value == compare)
	{	return message;		}
	else
	{	return "";		}
}
function checkfield2(myfield,message)/* without comparation*/
{	if (myfield.value.length == 0 || myfield.value == "" || myfield.value == "0")
	{	return message;		}
	else
	{	return "";		}
}
function checkfield3(myfield,message)/* to check numbers */
{	var string = myfield.value;
	string = delete_min(string);
	string = delete_space(string);
	
	if(isNaN(string))
	{	return message;		}
	else 
	{	return "";		}
}
function checkfield4(myfield,message)
{
	var flag1 = myfield.value.indexOf('@');
	var flag2 = myfield.value.indexOf('.');
	if( flag1==-1 || flag2==-1 || flag1>flag2 )
	{	
	  return message;
	}
	else return "";
}
//untuk ambil value dari url parameter, var value = gup('paramname');
function gup(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function FillVerification(value,id,focused,default_value)
{
	if(value==default_value)
	{
		var value = "Invalid Verification Code";	
		document.getElementById(id).innerHTML=value;
		document.getElementById(focused).focus();
	}
}


function validateForm_lama(formName) 
{	
	if (!CheckMandatoryCompare(formName.first_name,"Please Input FIRST NAME","First Name")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.last_name,"Please Input LAST NAME","Last Name")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.email,"Please Input EMAIL","Email Address")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.mobile,"Please Input MOBILE","Mobile Phone #")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.city,"Please Input CITY","City")){
	return false;
	}
	if (!CheckMandatory(formName.state,"Please Input STATE")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.zip,"Please Input ZIP","Zip")){
	return false;
	}
	if (!CheckMandatory(formName.project_type,"Please Input PROJECT TYPE")){
	return false;
	}
	
	if (!CheckCharacterFilter1('@',formName.email,"Please input VALID EMAIL ADDRESS")){
	return false;
	}
	if (!CheckCharacterFilter1('.',formName.email,"Please input VALID EMAIL ADDRESS")){
	return false;
	}
	if (!CheckCheckNumber(formName.mobile,"Please input A VALID MOBILE NUMBER")){
	return false;
	}
	if (!CheckCheckNumber(formName.zip,"Please input A VALID ZIP")){
	return false;
	}
formName.submit();
return true;
}

function validateForm(formName){
	var message = "";
	message = message + checkfield1(formName.first_name,"- Please input FIRST NAME\n\r","First Name");
	message = message + checkfield1(formName.last_name,"- Please input LAST NAME\n\r","Last Name");
	message = message + checkfield1(formName.email,"- Please input EMAIL ADDRESS\n\r","Email Address");
	message = message + checkfield1(formName.mobile,"- Please input MOBILE NUMBER\n\r","Mobile Phone #");
	message = message + checkfield2(formName.project_type,"- Please select PROJECT TYPE\n\r");
	if(checkfield1(formName.email,"- Please input EMAIL ADDRESS\n\r","Mobile Phone #")=="") { message = message + checkfield4(formName.email,"- Please input VALID EMAIL ADDRESS\n\r"); }
	if(checkfield1(formName.mobile,"- Please input MOBILE NUMBER\n\r","Mobile Phone #")=="") { message = message + checkfield3(formName.mobile,"- Please input a valid MOBILE NUMBER\n\r"); }
	if(message!="") 
	{ alert(message);
	  return false; 
	}
	else 
	{ formName.submit();
	}
}

function validateForms(formName) 
{	
	if (!CheckMandatoryCompare(formName.first_name,"Please Input FIRST NAME","First name")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.last_name,"Please Input LAST NAME","Last name")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.emailbox,"Please Input EMAIL ADDRESS","Email Address")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.mobile_phone,"Please Input MOBILE PHONE","Mobile Phone #")){
	return false;
	}
	if (!CheckMandatory(formName.subject,"Please Input SUBJECT")){
	return false;
	}
	if (!CheckMandatory(formName.message,"Please Input MESSAGE")){
	return false;
	}
	
	if (!CheckCharacterFilter1('@',formName.emailbox,"Please input VALID EMAIL ADDRESS")){
	return false;
	}
	if (!CheckCharacterFilter1('.',formName.emailbox,"Please input VALID EMAIL ADDRESS")){
	return false;
	}
	if (!CheckCheckNumber(formName.mobile_phone,"Please input A VALID MOBILE PHONE")){
	return false;
	}
formName.submit();
return true;
}

function validateRepForm(formName) 
{	
	if (!CheckMandatoryCompare(formName.first_name,"Please Input FIRST NAME","First name")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.last_name,"Please Input LAST NAME","Last name")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.emailbox,"Please Input EMAIL ADDRESS","Email Address")){
	return false;
	}
	if (!CheckMandatoryCompare(formName.mobile_phone,"Please Input MOBILE PHONE","Mobile Phone #")){
	return false;
	}
	if (!CheckMandatory(formName.message,"Please Input MESSAGE")){
	return false;
	}
	
	if (!CheckCharacterFilter1('@',formName.emailbox,"Please input VALID EMAIL ADDRESS")){
	return false;
	}
	if (!CheckCharacterFilter1('.',formName.emailbox,"Please input VALID EMAIL ADDRESS")){
	return false;
	}
	if (!CheckCheckNumber(formName.mobile_phone,"Please input A VALID MOBILE PHONE")){
	return false;
	}
formName.submit();
return true;
}

