function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
    }
	
	function populateSubCategory(selectedSubCategory) {		
		//alert(document.getElementById('category').value);
		//alert(selectedSubCategory);
		var strURL="modules/GetSubCategory.php?category="+document.getElementById('category').value+"&selectedSubCategory="+selectedSubCategory;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {
						document.getElementById('subCategoryDiv').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}		
	}
	



String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

/*function populateSubCategory(formObj)
{
	alert("hi " + $selected_category_id);
	
	document.postCompleteAd.selected_category_id.value = document.getElementById('category').options[document.getElementById('category').options.selectedIndex].value;
	alert(document.postCompleteAd.selected_category_id.value);
	//self.location = 'index.php?p=postAnAd&cat=' + val;
	//document.postCompleteAd.elements['subCategory'].options[0] = new Option("hello", "hello");
}*/
function hidediv(parentid) 
{
	if (document.getElementById)  // DOM3 = IE5, NS6
	{
		if(document.getElementById(parentid).style.visibility == 'hidden')
		{
			document.getElementById(parentid).style.visibility = 'visible';
		}
		else
		{
			document.getElementById(parentid).style.visibility = 'hidden';
		}
	}
	else 
	{
		if (document.layers) { // Netscape 4
			if(document.hideshow.visibility == 'visible')
			{
				document.hideshow.visibility = 'hidden';
			}
			else
			{
				document.hideshow.visibility = 'visible';
			}
		}
		else 
		{ // IE 4
			if(document.all.hideshow.style.visibility == 'visible')
			{
				document.all.hideshow.style.visibility = 'hidden';
			}
			else
			{
				document.all.hideshow.style.visibility = 'visible';
			}
		}
	}
}

var state = 'none';

function showhide(layer_ref) {
	if (state == 'block') {
		state = 'none';
	}
	else {
		state = 'block';
	}
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + layer_ref + ".style.display = state");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[layer_ref].display = state;
	}
	if (document.getElementById &&!document.all) {
		hza = document.getElementById(layer_ref);
		hza.style.display = state;
	}
} 
/** not used any more
 * 
 * @return
 */
/*function displayOtherCategoryName()
{
	alert("hello " + document.getElementById("category").value);
	if(document.getElementById("category").value == "1001")
	{
		document.getElementById("otherCategoryName").style.display = "block";
	}
	else
	{
		document.getElementById("otherCategoryName").style.display = "none";
	}
}*/


function validate()
{
	var errors="";
	if(document.postAnAd.elements['advtPurpose'].value <= 0)
	{
		errors = errors + "Please select what you want to do \n";
	}
	if(document.postAnAd.elements['location'].value <= 0)
	{
		errors = errors + "Please select a location \n";
	}
	if(document.postAnAd.elements['category'].value <= 0)
	{
		errors = errors + "Please select a category \n";
	}
	if(document.postAnAd.elements['name'].value.trim().length <= 0)
	{
		errors = errors + "Please input your name \n";
	}
	if(document.postAnAd.elements['mobileNumber'].value.trim().length <= 0)
	{
		errors = errors + "Please input your mobile number \n";
	}
	if(document.postAnAd.elements['mobileNumber'].value.trim().length >0 && isNumeric(document.postAnAd.elements['mobileNumber'].value, "0123456789-") == false)
	{
		errors = errors + "Please input a valid mobile number \n";
	}
	var advtTypeVal = "";
	for(var i=0; i<document.postAnAd.advtType.length; i++)
	{
		if(document.postAnAd.advtType[i].checked)
		{
			advtTypeVal = document.postAnAd.advtType[i].value;
		}
	}
	if(advtTypeVal == "")
	{
		errors = errors + "Please select Ad type - free or premium \n";
	}
	if( errors == "")
	{
		return true;
	}
	else
	{
		alert(errors);
		return false;
	}
}


/** Function emailcheck is used to verify if the given value is a possible valid email address. 
 * This function thus simply makes sure the email address has one (@), atleast one (.).
 * It also makes sure that there are no spaces, extra '@'s or a (.) just before or after the @.
 * It also makes sure that there is atleast one (.) after the @. 
 */ 
function emailcheck($email) {
	var at="@";
	var dot=".";
	$email=$email.trim();
	var atIndex=$email.indexOf(at);
	var emailLength=$email.length;
	var dotIndex=$email.indexOf(dot);
	if ($email.indexOf(at)==-1){
	   return false;
	}
	if ($email.indexOf(at)==-1 || $email.indexOf(at)==0 || $email.indexOf(at)==emailLength){
	   return false;
	}
	if ($email.indexOf(dot)==-1 || $email.indexOf(dot)==0 || $email.indexOf(dot)==emailLength){
	    return false;
	}
	if ($email.indexOf(at,(atIndex+1))!=-1){
		return false;
	}
	if ($email.substring(atIndex-1,atIndex)==dot || $email.substring(atIndex+1,atIndex+2)==dot){
	    return false;
	}
	if ($email.indexOf(dot,(atIndex+2))==-1){
	    return false;
	}
	if ($email.indexOf(" ")!=-1){
	    return false;
	}
	return true;					
}
/** not working as expected*/
function checkEmail($email){
	return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
}

function validateCompleteAd()
{
	var errors="";
	if(document.postCompleteAd.elements['advtPurpose'].value <= 0)
	{
		errors = errors + "Please select what you want to do \n";
	}
	if(document.postCompleteAd.elements['category'].value <= 0)
	{
		errors = errors + "Please select a category \n";
	}
	if(document.postCompleteAd.elements['subCategory'].value <= 0)
	{
		errors = errors + "Please select a sub category \n";
	}
	if(document.postCompleteAd.elements['name'].value.trim().length <= 0)
	{
		errors = errors + "Please input your name \n";
	}
	if(document.postCompleteAd.elements['mobileNumber'].value.trim().length > 0 && isNumeric(document.postCompleteAd.elements['mobileNumber'].value, "0123456789-") == false)
	{
		errors = errors + "Please input a valid mobile number \n";
	}
	var advtTypeVal = "";
	for(var i=0; i<document.postCompleteAd.advtType.length; i++)
	{
		if(document.postCompleteAd.advtType[i].checked)
		{
			advtTypeVal = document.postCompleteAd.advtType[i].value;
		}
	}
	if(advtTypeVal == "")
	{
		errors = errors + "Please select Ad type - free or premium \n";
	}
	if(document.postCompleteAd.elements['title'].value.trim().length <= 0)
	{
		errors = errors + "Please input the title of your ad \n";
	}
	if(document.postCompleteAd.elements['emailID'].value.trim().length > 0 && emailcheck(document.postCompleteAd.elements['emailID'].value.trim()) != true)
	{
		errors = errors + "Please input a valid email id \n";
	}
	if(document.postCompleteAd.elements['emailID'].value.trim().length > 0 && document.postCompleteAd.elements['emailIDConfirm'].value.trim().length > 0 && document.postCompleteAd.elements['emailID'].value.trim() != document.postCompleteAd.elements['emailIDConfirm'].value.trim())
	{
		errors = errors + "Your email ids donot match \n";
	}
	if(document.postCompleteAd.elements['price'].value.trim().length > 0 && isNumeric(document.postCompleteAd.elements['price'].value, "0123456789.,") == false)
	{
		errors = errors + "Please input a valid price \n";
	}
	if( errors == "")
	{
		return true;
	}
	else
	{
		alert(errors);
		return false;
	}
}


function validateLogin()
{
	var errors="";
	if(document.login.elements['userid'].value.trim().length <= 0)
	{
		errors = errors + "Please input your email id \n";
	}
	if(document.login.elements['userid'].value.trim().length > 0 && emailcheck(document.login.elements['userid'].value.trim()) != true)
	{
		errors = errors + "Please input a valid email id \n";
	}
	if(document.login.elements['passwd'].value.trim().length <= 0)
	{
		errors = errors + "Please input your password \n";
	}
	
	if( errors == "")
	{
		return true;
	}
	else
	{
		alert(errors);
		return false;
	}
}

/*  prev used for othercategory*/
function clearInput(formName, fieldName)
{
	value='';
	eval("document." + formName + ".elements[fieldName].value=value");
}
// FUNCTION NOT IN USE
function isEmpty()
{
	var errors="";
	if(document.search.elements['cat_id'])
	{
		if(document.search.elements['searchTxt'].value.trim().length <= 0 && document.search.elements['cat_id'].value <=0)
		{
			errors = errors + "Please input the search criterion \n";
		}
	}	
	else
	{
		if(document.search.elements['searchTxt'].value.trim().length <= 0)
		{
			errors = errors + "Please input the search text \n";
		}
	}
	if( errors == "")
	{
		return true;
	}
	else
	{
		alert(errors);
		return false;
	}
}

function validateEmailId()
{
	var errors="";
	if(document.frmForgotPassword.elements['emailID'].value.trim().length <= 0)
	{
		errors = errors + "Please input the login email id \n";
	}
	if(document.frmForgotPassword.elements['emailID'].value.trim().length > 0 && emailcheck(document.frmForgotPassword.elements['emailID'].value.trim()) != true)
	{
		errors = errors + "Please input a valid email id \n";
	}
	
	if( errors == "")
	{
		return true;
	}
	else
	{
		alert(errors);
		return false;
	}
}

function validateRegistrationForm()
{
	var errors="";
	if(document.frmRegistration.elements['emailid'].value.trim().length <= 0)
	{
		errors = errors + "Please input your Login Email Id \n";
	}
	if(document.frmRegistration.elements['emailid'].value.trim().length > 0 && emailcheck(document.frmRegistration.elements['emailid'].value.trim()) != true)
	{
		errors = errors + "Please input a valid email id \n";
	}
	if(document.frmRegistration.elements['passwd'].value.trim().length <= 0)
	{
		errors = errors + "Please input your Password \n";
	}
	if(document.frmRegistration.elements['confirmPasswd'].value.trim().length <= 0)
	{
		errors = errors + "Please confirm your password \n";
	}
	if(document.frmRegistration.elements['passwd'].value.trim().length > 0 && document.frmRegistration.elements['confirmPasswd'].value.trim().length > 0 
			&& document.frmRegistration.elements['passwd'].value.trim() != document.frmRegistration.elements['confirmPasswd'].value.trim())
	{
		errors = errors + "Your Passwords donot match \n";
	}
	if(document.frmRegistration.elements['postalCode'].value.trim().length <= 0)
	{
		errors = errors + "Please input your Postal Code \n";
	}
	if(document.frmRegistration.elements['postalCode'].value.trim().length > 0 && isNumeric(document.frmRegistration.elements['postalCode'].value.trim(), "0123456789") == false)
	{
		errors = errors + "Please input a valid Postal Code \n";
	}
	if(!document.frmRegistration.tnc.checked)
	{
		errors = errors + "Please accept the terms of service \n";
	}
	if( errors == "")
	{
		return true;
	}
	else
	{
		alert(errors);
		return false;
	}
}

function advtTypeSelected(radio)
{
	var value = radio.value;
	//var frmFile = document.getElementById("frmFileUpload");
	var frmFile = document.getElementById("uploadFilesPlaceHolder");
	
	//Node fileNodes
	
	while( frmFile.hasChildNodes())
	{
		frmFile.removeChild(frmFile.firstChild);
	}
	if(value == "free")
	{
		var label = document.createElement('LABEL');
		label.setAttribute("for", "imageFile");
		frmFile.appendChild(label);
		var labelValue = document.createTextNode('Image ');
		label.appendChild(labelValue);
		var element = document.createElement("input");
		element.style.height="22px";
		element.setAttribute("type", "file");  
		element.setAttribute("name", "imageFile[]");
		frmFile.appendChild(element);  
	}
	else if(value == "premium")
	{
		for(i=1; i<=6; i++)
		{
			var label = document.createElement('LABEL');
			label.setAttribute("for", "imageFile");
			frmFile.appendChild(label);
			
			var labelValue = document.createTextNode("Image " + i + " ");
			frmFile.appendChild(labelValue);
			
			var element = document.createElement("input");
			element.style.height="22px";
			element.setAttribute("type", "file");  
			element.setAttribute("name", "imageFile[]");
			frmFile.appendChild(element);
			
			var br1 = document.createElement('BR');
			frmFile.appendChild(br1);
			var br2 = document.createElement('BR');
			frmFile.appendChild(br2);
		}
	}
}
function callPrint()
{
	window.print();
}

function printCoupon(imgId)
{
	pwin = window.open(document.getElementById(imgId).src,"_blank");
	pwin.onload = callPrint();
}

function showPopup(url) { 
	newwindow=window.open(url,'name','height=190,width=520,top=200,left=300,resizable'); 
	if (window.focus) {newwindow.focus();} 
}


function isNumeric(strString, strValidChars)
//  check for valid numeric strings	
{
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
   {
   strChar = strString.charAt(i);
   if (strValidChars.indexOf(strChar) == -1)
      {
      blnResult = false;
      }
   }
return blnResult;
}
