// file path: X:\WEBSITES\shaaditimes.com\dev\wedding-directory\ssi\directory-script.js
// #### reset form  ###
function reset_form(sForm){
		
		 _oDF = document.forms[sForm];

		 _oDF.reset();
		 _oDF.counter.value=_oDF.company_description.value.length;
		return  _oDF.counter.value;
		
}


// #### clear form varaible value ###
function cls(str){
	str.value = "";
	return;
}


// directory function for selecting state according country
function ifcountry_india_usa(oCountry, sForm)
{
	var _oDF = document.frmregister;

	if(sForm) _oDF = document.forms[sForm];

	if(oCountry.options[oCountry.selectedIndex].text == 'India')
	{
		_oDF.indianstates.disabled = false;
		_oDF.usstates[0].selected  = true;
		_oDF.usstates.disabled     = true;
		_oDF.otherstate.value      = "";
		_oDF.otherstate.disabled   = true;
		return true;
	}
	else
	{
		if(oCountry.options[oCountry.selectedIndex].text == 'USA')
		{
			_oDF.indianstates[0].selected = true;
			_oDF.indianstates.disabled    = true;
			_oDF.usstates.disabled        = false;
			_oDF.otherstate.value         = "";
			_oDF.otherstate.disabled      = true;
			return true;
		}
		else
		{
			_oDF.otherstate.disabled      = false;
			//_oDF.otherstate.focus();
			_oDF.usstates[0].selected     = true;
			_oDF.usstates.disabled        = true;
			_oDF.indianstates[0].selected = true;
			_oDF.indianstates.disabled    = true;
			return true;
		}
	}
}


function ifcountry_india_uk(oCountry, city,sForm)
{//alert(oCountry);
//alert(city);
//alert(sForm);
	var _oDF;
	if(sForm){
		_oDF = document.forms[sForm];
	}
	else
	{
		_oDF = document.frmregister;
	}
	if(oCountry == 'India')
	{
		_oDF.othercountry[0].selected = true;
		_oDF.othercountry.disabled    = true;
		document.getElementById('otherRow').style.display="none";
		_oDF.indianstates.disabled = false;
		document.getElementById('stateRow').style.display = "";
		_oDF.indianstates.disabled   = false;
		document.getElementById('countyRow').style.display="none";
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		document.getElementById('UsastateRow').style.display="none";
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
		showCity('country',oCountry,city);
		//showCategory(oCountry.options[oCountry.selectedIndex].text);
		
	}
	else if(oCountry == 'United Kingdom')
	{
		_oDF.othercountry[0].selected = true;
		_oDF.othercountry.disabled    = true;
		document.getElementById('otherRow').style.display="none";
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		document.getElementById('stateRow').style.display="none";
		document.getElementById('countyRow').style.display="";
		_oDF.county.disabled   = false;	
		document.getElementById('UsastateRow').style.display="none";
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
		showCity('country',oCountry, city);
		//showCategory(oCountry.options[oCountry.selectedIndex].text);
		
	}
	else if(oCountry == 'United Arab Emirates')
	{
		_oDF.othercountry[0].selected = true;
		_oDF.othercountry.disabled    = true;
		document.getElementById('otherRow').style.display="none";
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		document.getElementById('stateRow').style.display="none";
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		document.getElementById('countyRow').style.display="none";
		document.getElementById('UsastateRow').style.display="none";
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
		showCity('country',oCountry,city);
		//showCategory(oCountry.options[oCountry.selectedIndex].text);
	}
	else if(oCountry == 'USA')
	{
		_oDF.othercountry[0].selected = true;
		_oDF.othercountry.disabled    = true;
		document.getElementById('otherRow').style.display="none";
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		document.getElementById('stateRow').style.display="none";
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		document.getElementById('countyRow').style.display="none";
		showCity('country',oCountry,city);
		document.getElementById('UsastateRow').style.display="";
		_oDF.usastates.disabled   = false;
		showCity('country',oCountry,city);
	}
	else if(oCountry == 'Others')
	{
		_oDF.othercountry[0].selected = false;
		_oDF.othercountry.disabled    = false;
		document.getElementById('otherRow').style.display="";
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		document.getElementById('stateRow').style.display="none";
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		document.getElementById('countyRow').style.display="none";
		document.getElementById('UsastateRow').style.display="none";
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
		showCity('country',oCountry,city);
	}
	else
	{
		_oDF.othercountry[0].selected = true;
		_oDF.othercountry.disabled    = true;
		document.getElementById('otherRow').style.display="none";
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		document.getElementById('stateRow').style.display="none";
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		document.getElementById('countyRow').style.display="none";
		document.getElementById('UsastateRow').style.display="none";
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
		//showCity('country',oCountry,city);
	}
	
}

function ifcountry_india_uk_billing(oCountry, sForm)
{
	var _oDF = document.frmregister;

	if(sForm) _oDF = document.forms[sForm];
	if(oCountry == 'India')
	{
		_oDF.indianstates.disabled   = false;
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
		
	}
	else if(oCountry == 'United Kingdom')
	{
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		_oDF.county.disabled   = false;		
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
		
	}
	else if(oCountry == 'USA')
	{
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		_oDF.usastates.disabled   = false;
		
	}
	else
	{
		_oDF.indianstates[0].selected = true;
		_oDF.indianstates.disabled    = true;
		_oDF.county[0].selected = true;
		_oDF.county.disabled   = true;
		_oDF.usastates[0].selected = true;
		_oDF.usastates.disabled   = true;
	}
}

function ifcategory_other(oCategory)
{
	var _oDF = document.frmregister;

	//if(sForm) _oDF = document.forms[sForm];
	if(oCategory != 'Other')
	{
		document.getElementById('othercategory').value	= "";
		document.getElementById('othercategory').disabled    = true;
		document.getElementById('othercatRow').style.display="none";
		
	}
	else if(oCategory == 'Other')
	{
		document.getElementById('othercategory').disabled    = false;
		document.getElementById('othercatRow').style.display="";
		
	}
}

/*
function to show advance search fields on click
 * @usage  <a onclick="show_advance_search();">

*/
var showhide="show";
function show_advance_search()
{
	if(showhide=="show")
	{
		document.getElementById('advancesearch_row').style.display = "";
		showhide="hide";
	}
	else if(showhide == "hide")
	{
		document.getElementById('advancesearch_row').style.display = "none";
		showhide="show";
	}

}

function clear_text(id,def,val)
{	
	if(def == val)
	{
	document.getElementById(id).value='';	
	}
}



function show_text(id,def,val)
{	
	if(val == "")
	{
	document.getElementById(id).value=def;	
	}
	else if(def != val)
	{
	advance_search(id);
	}
}

function search_form(id)
{
	if(document.getElementById(id).value !='')
	{
		if(id == "city")
		{
			//--Disable other controls
			document.getElementById('city').disabled =false;
			document.getElementById('county').disabled = true;
			
			//----clear values
			document.getElementById('county').value = "";
		}
		else if(id == "county")
		{
			//--Disable other controls
			document.getElementById('city').disabled =true;
			document.getElementById('county').disabled = false;

			//----clear values
			document.getElementById('city').value = "";
		}
	}
	else
	{
		document.getElementById('city').disabled =false;
		document.getElementById('county').disabled = false;
		//----clear values
		document.getElementById('city').value = "";
		document.getElementById('county').value = "";
	}
}
function advance_search(id)
{	
	document.getElementById('type').value = id;
	document.getElementById('searchval').value = document.getElementById(id).value;

	if(document.getElementById(id).value == "")
	{
		document.getElementById('pincode').disabled  = false;
		document.getElementById('locality').disabled = false;
		document.getElementById('keyword').disabled  = false;
	}
	else
	{
		if(id == "pincode")
		{
			//--Disable other controls
			document.getElementById('locality').disabled =true;
			document.getElementById('keyword').disabled = true;
			
			//----clear values
			document.getElementById('locality').value = "";
			document.getElementById('keyword').value = "";
		}
		else if(id == "locality")
		{
			//--Disable other controls
			document.getElementById('pincode').disabled =true;
			document.getElementById('keyword').disabled = true;

			//----clear values
			document.getElementById('pincode').value = "";
			document.getElementById('keyword').value = "";
		}
		else if(id == "keyword")
		{
			//--Disable other controls
			document.getElementById('pincode').disabled =true;
			document.getElementById('locality').disabled = true;

			//----clear values
			document.getElementById('pincode').value = "";
			document.getElementById('locality').value = "";
		}
		
	}
}

/**
 * counter for textarea
 *
 * @author Ritchie
 * @param  string  sForm      form name
 * @param  string  sTextArea  textarea name
 * @param  string  sTextInput counter name
 * @param  integer iMaxLimit  max number for counter
 *
 * @usage  <textarea onKeyUp="calcCharLeft('frmregister', 'company_description', 'counter', 150)">
*/
function calcCharLeft(sForm, sTextArea, sTextInput, iMaxLimit)
{
	var _oDF        = document.forms[sForm];
	var _oTxtA      = _oDF.elements[sTextArea];
	var _iMaxLength = (!iMaxLimit) ? 100 : iMaxLimit;
	var _iCharLeft  = _oTxtA.value.length;

	_oDF.elements[sTextInput].value = _iCharLeft;
	//alert(_iCharLeft);
	if(_iCharLeft > _iMaxLength)
	{
		_oTxtA.value = _oTxtA.value.substring(0, _iMaxLength);
		_oDF.elements[sTextInput].value = _iMaxLength;
		alert('You can enter only '+_iMaxLength+' characters.');
	}
}

/* --- Ajax function to show city list accoring to selected country-----*/
function showCity(type,str,city)
{
if (str.length==0)
  { 
  document.getElementById("cityid").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/ssi/city-array.php";
url=url+"?value="+str;
url=url+"&type="+type;
url=url+"&city="+city;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}

function stateChanged() 
{
	if (xmlHttp.readyState==4)
	{
		document.getElementById('cityid').innerHTML=xmlHttp.responseText;
	}
	else
	{
		document.getElementById('cityid').innerHTML="";
	}
}

// function used in vendor listing backed 
function getVendorByTelephone(telephone)
{
	// check telephone number is entered or not
	if(!telephone)
	{
		alert("Please Enter Telephone number");
		return false;
	}

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return false;
	}

	var url="/administrator/dir/list-vendor/vendor-data.php";
	url=url+"?telephone="+telephone;
	xmlHttp.onreadystatechange=fillUpVendorForm;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function fillUpVendorForm()
{
	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.responseText != " ")
		{
			jStr = xmlHttp.responseText;
			var jObj = eval('(' + jStr + ')');
			conArr = jObj.options;
			var answer;
			var lastlogindate = "-";
			var url = "";

			if(conArr[0].telephone != document.frmregister.telephone.value)
			{
				var isUpdate;
				isUpdate = confirm("Record with this number already exist with different std code.\Would you like to update existing one or add new entry.\n click OK for update OR cancel for add new entry.");
				if(isUpdate == true)
				{
					document.frmregister.telephone.value = conArr[0].telephone;
				}
				else//add record with entered telephone number.
				{
					setEmpty();
					document.frmregister.login.focus();
					return true;
				}
			}

			if(conArr[0].lastlogin)
			{
				lastlogindate = conArr[0].lastlogin;
			}
			if(conArr[0].status == "Active") // active
			{
				answer = confirm("The Listing already exists in site.\nclick ok for view and update or Cancel to enter another record\n\nMembership Type : "+conArr[0].account_type+"\nInserted Date : "+conArr[0].recorddate+"\nLast Login Date : "+lastlogindate);
			}
			else if(conArr[0].status == "New") // screening
			{
				answer = confirm("The Listing is already exists in screening.\nclick ok for view and update screening or Cancel to enter another record\n\nMembership Type : "+conArr[0].account_type+"\nInserted Date : "+conArr[0].recorddate+"\nLast Login Date : "+lastlogindate);
			}
			else if(conArr[0].status == "Deactive")
			{
				answer = confirm("The Listing is already exists but deactive.\nclick ok for view and update or Cancel to enter another record\n\nMembership Type : "+conArr[0].account_type+"\nInserted Date : "+conArr[0].recorddate+"\nLast Login Date : "+lastlogindate);
			}

			if(answer)
			{
				document.frmregister.login.value = conArr[0].login;
				document.frmregister.password1.value = conArr[0].password;
				document.frmregister.password2.value = conArr[0].password;
				document.frmregister.salutation.value = conArr[0].salutation;
				document.frmregister.name.value = conArr[0].contact_person; // contact name
				document.frmregister.country.value = conArr[0].country; 
				
				// Sid - Repopulate dropdowns			
				if(conArr[0].country == "India")
				{
					ifcountry_india_uk(conArr[0].country,conArr[0].city);
					showCity('state',conArr[0].state,conArr[0].city)
					document.frmregister.indianstates.value = conArr[0].state;
					
				}else if(conArr[0].country == "USA")
				{
					ifcountry_india_uk(conArr[0].country,conArr[0].city);
					showCity('state',conArr[0].state,conArr[0].city)
					document.frmregister.usastates.value = conArr[0].state;
				}else if(conArr[0].country == "United Kingdom")
				{
					ifcountry_india_uk(conArr[0].country,conArr[0].city);
					showCity('state',conArr[0].county,conArr[0].city)
					document.frmregister.county.value = conArr[0].county;
				}else{
					ifcountry_india_uk("Others",conArr[0].city);
					showCity('nostate',conArr[0].country,conArr[0].city)
					document.frmregister.country.value = "Others";
					document.frmregister.othercountry.value = conArr[0].country;
				}

				document.frmregister.address1.value = conArr[0].address1;
				document.frmregister.address2.value = conArr[0].address2;
				document.frmregister.locality.value = conArr[0].locality;
				document.frmregister.landmark.value = conArr[0].landmark;
				document.frmregister.zip.value = conArr[0].zip;
				document.frmregister.mobile.value = conArr[0].mobile;
				document.frmregister.fax.value = conArr[0].fax;
				document.frmregister.email.value = conArr[0].email;
				if(conArr[0].url)
				{
					document.frmregister.url.value = conArr[0].url.substring(conArr[0].url.indexOf("://")+3,conArr[0].url.length);
				}
				document.frmregister.company_name.value = conArr[0].branch_name;
				document.frmregister.company_description.value = conArr[0].branch_description;
				if(conArr[0].logo)
				{
					myImage = new Image();
					myImage.src = conArr[0].logo;
					document["active_logo"].src = myImage.src;
					document.getElementById("active_logo_id").style.display = "block";
					document.frmregister.active_logo.value = conArr[0].logo;
				}
				document.frmregister.subcategory_id.text = conArr[0].subcategory;
				document.frmregister.subcategory_id.value = conArr[0].subcategory_id+"|"+conArr[0].category_id;
				document.frmregister.acceptterms.checked = true;
				document.frmregister.update.value = "update";
				document.frmregister.vendorid.value = conArr[0].id;
			}
			else
			{
				window.location.href="registration.php";
			}
		}
		else
		{
			alert("No Record.");
			setEmpty();
		}
	}
}
function setEmpty()
{
	document.frmregister.login.value = "";
	document.frmregister.password1.value = "";
	document.frmregister.password2.value = "";
	document.frmregister.salutation.value = "";
	document.frmregister.name.value = ""; // contact name
	document.frmregister.country.value = ""; 
	document.frmregister.indianstates.value = "";
	document.getElementById('cityid').innerHTML="";
	document.frmregister.address1.value = "";
	document.frmregister.address2.value = "";
	document.frmregister.locality.value = "";
	document.frmregister.landmark.value = "";
	document.frmregister.zip.value = "";
	document.frmregister.mobile.value = "";
	document.frmregister.fax.value = "";
	document.frmregister.email.value = "";
	document.frmregister.url.value = "";
	document.frmregister.company_name.value = "";
	document.frmregister.company_description.value = "";
	document.frmregister.subcategory_id.value = "";
	document.frmregister.acceptterms.checked = false;
	document.frmregister.update.value = "";
	document.frmregister.vendorid.value = "";
	document.getElementById("active_logo").value = "";
}