var erroru = "";
var errore = "";
var Error = "";
var base_url = "http://www.contemporarybride.com/";
var img_url = "http://www.contemporarybride.com/";
var login_type = '';
var ltype = '';
var strError = '';

function $$(e){
	if(typeof e=='string')e=document.getElementById(e);
	return e;
};
function collect(a,f){
	var n=[];
	for(var i=0;i<a.length;i++){
		var v=f(a[i]);
		if(v!=null)n.push(v)
	}
	return n
};

ajax={};
ajax.x=function(){
	try{
		return new ActiveXObject('Msxml2.XMLHTTP')
	}catch(e){
		try{
			return new ActiveXObject('Microsoft.XMLHTTP')
		}catch(e){
			return new XMLHttpRequest()
		}
	}
};
ajax.serialize=function(f){
	var g=function(n){
		return f.getElementsByTagName(n)
	};
	var nv=function(e){
		if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);
		else return ''
	};
	var i=collect(g('input'),
	function(i){
		if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');
};
ajax.send=function(u,f,m,a){
	var x=ajax.x();
	x.open(m,u,true);
	x.onreadystatechange=function(){
		if(x.readyState==4)f(x.responseText)
	};
	if(m=='POST')
	x.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	x.send(a)
};
ajax.get=function(url,func){
	ajax.send(url,func,'GET')
};
ajax.gets=function(url){
	var x=ajax.x();
	x.open('GET',url,false);
	x.send(null);
	return x.responseText
};
ajax.post=function(url,func,args){
	ajax.send(url,func,'POST',args);
};
ajax.update=function(url,elm){
	var e=$$(elm);
	var f=function(r){
		e.innerHTML=r
	};
	ajax.get(url,f)
};
ajax.submit=function(url,elm,frm){
	var e=$$(elm);
	var f=function(r){
		e.innerHTML=r;
	};
	ajax.post(url,f,ajax.serialize(frm));
};

function clearForm(formIdent) 
{ 
  	var form, elements, i, elm; 
  	form = document.getElementById ? document.getElementById(formIdent) : document.forms[formIdent]; 
	if (document.getElementsByTagName)
	{
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); )
		{
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
		}
	}

	// Actually looking through more elements here
	// but the result is the same.
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
		}
	}
}

function dismissErrMsg(){
	document.getElementById('err_message').innerHtml='';
  	document.getElementById("err_message_container").style.display = "none";	
}

function windowClose(elm){
	document.getElementById(elm).innerHtml='';
  	document.getElementById(elm).style.display = "none";	
}

function alertMsg(msg){
	alert(msg);
}

function bookmarkPhoto(url){
	ajax.get(base_url + './handlers/bookmark.php?type=photo&url=' + url, alertMsg);
}

function bookmarkVendor(id){
	ajax.get(base_url + 'handlers/bookmark.php?type=vendor&id=' + id, alertMsg);
}

function reportPhoto(name){
	ajax.get(base_url + './handlers/report.php?type=photo&name=' + name, alertMsg);
}

function bookmarkThread(tid){
	ajax.get(base_url + './handlers/bookmark.php?type=thread&tid=' + tid, alertMsg);
}

function reportThread(tid){
	ajax.get(base_url + './handlers/report.php?type=thread&tid=' + tid, alertMsg);
}

function vendor_link(vid){
	ajax.gets(base_url + './handlers/bookmark.php?tid=' + vid);
}

function captcha(){
	ajax.update(base_url + './handlers/register.php?action=captcha', 'captcha_div');
}

function rateBlock(id){
	document.getElementById('rate_div').innerHTML='';
  	document.getElementById("rate_div").style.display = "block";
	ajax.update('../handlers/vendor_rate.php?id=' + id, 'rate_div');
}

function userMail(type){
	document.getElementById('user_search_div').innerHTML='';
  	document.getElementById("user_search_div").style.display = "block";
	ajax.update('../handlers/locate_user.php?type=' + type, 'user_search_div');
}

function searchUserMail(form){
	ajax.submit('../handlers/locate_user.php',"user_search_div",form);
}

function setUserMail(id, name){
	document.getElementById('user_div').innerHTML=name;
	document.getElementById('user_id').value=id;
  	document.getElementById("user_search_div").style.display = "none";
}

function rateVendor(form){
	ajax.submit('../handlers/vendor_rate.php',"rate_div",form);
}

function rateResponse(response){
	document.getElementById('rate_div').innerHTML=response;
}


function new_forum(type){
	ajax.update(base_url + 'handlers/new_forum.php?type=' + type, 'new_group_div');
}

function editForum(type, id){
	ajax.update(base_url + 'handlers/new_forum.php?action=edit&type=' + type + '&id=' + id, 'new_group_div');
}


function edit_group(id){
	ajax.update('./handlers/new_group_form.php?action=edit&id=' + id, 'new_group_div');
}

function new_event(){
	ajax.update('./handlers/new_event_form.php', 'new_event_div');
}

function selectMailBox(box){
	if(box != ''){
		document.getElementById("box_contents_main").style.display="block";
		ajax.update('./handlers/mail.php?action=' + box, 'box_contents');
	}else{
		document.getElementById("box_contents_main").style.display="none";
	}
}

function hfields(){
	ajax.update('./handlers/register.php?action=hfields', 'hfields_div');	
}

function Msg(message){
	document.getElementById('err_message').innerHTML=message;
  	document.getElementById("err_message_container").style.display = "block";
	var pattern=/SUCCESS!!/i;
	if(pattern.test(message)){
		scroll(0,0);
	}	
}

function Login(form){
	ajax.post('./handlers/login.php',loginResponse,ajax.serialize(form) + '&login=true');
}

function loginResponse(response){
	var pattern=/ERROR/i;
	if(pattern.test(response)){
		Msg('<font color="red"><b>' + response + '</b></font>');
	}else{
		window.location = response;
	}
}

function resetPassword(form){
	ajax.post('./handlers/password.php',passwordResponse,ajax.serialize(form));
}

function passwordResponse(response){
	Msg('<font color="red"><b>' + response + '</b></font>');
}

function transBody(){
	document.getElementById('main_body').style.filter='alpha(opacity=30)'; 
	document.getElementById('main_body').style.mozOpacity='0.3'; 
	document.getElementById('main_body').style.opacity='0.3';
	document.getElementById('main_body').style.khtmlOpacity='.3'; 

}

function passRecoverForm(rtype){
	document.getElementById('pass_recovery_div').style.display='block';
	document.getElementById('login_div').style.display='none';
	transBody();
	scroll(0,0);
		// Open PHP script for requests
   		httpu2.open('get', base_url + 'handlers/pass_recovery.php?rtype=' + rtype, false);
		httpu2.send(null);
		if(httpu2.status == 200){
			// Text returned FROM the PHP script
      		var response = httpu2.responseText;
			if(response) {
				document.getElementById("pass_recovery_div").innerHTML = response;
      		}
		}
}

function recoverPass(rtype,form){
	transBody();
	document.getElementById('pass_recovery_div').style.display='block';
	scroll(0,0);
	// Open PHP script for requests
   	httpu2.open('POST', base_url + 'handlers/pass_recovery.php', false);
    httpu2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     httpu2.send('remail=' + form.remail.value + '&rtype=' + rtype);
		if(httpu2.status == 200){
			// Text returned FROM the PHP script
      		var response = httpu2.responseText;
			if(response) {
				document.getElementById("pass_recovery_div").innerHTML = response;
      		}
		}
}

function editWindow(url, width, height){
var str="";
 str += "height=" + height;
  str += ",innerHeight=" + height;
  str += ",width=" + width;
  str += ",innerWidth=" + width;
 str += ",menubar=1";
  str += ",resizable=1";
  str += ",scrollbars=1";

 window.open (url,"editwindow",str);
}

//Forum Stuff

    function confirmDelete(start, del, mNr, tNr, show) {
      var check = confirm("<? echo $message[14]; ?>");
      if(check) {
        var param = '?start=' + start + '&delete=' + del;
        if(mNr) param += '&mNr=' + mNr + '&tNr=' + tNr;
        if(show) param += '&date_show=' + show;
        document.location.href = '<? echo $PHP_SELF; ?>' + param;
      }
    }

function insertSmilie(txt) {
  var el = document.getElementById("message");
  if(!el.value) el.value = txt + ' ';
  else el.value += ((el.value.charAt(el.value.length-1) == ' ') ? '' : ' ') + txt + ' ';
  el.focus();
}

function countdown(cnt) {
  var obj = 0;

  if(document.getElementById) obj = document.getElementById('divSubmit');
  else if(document.all) obj = document.all.divSubmit;

  if(obj) {
    if(cnt < 1) {
      obj.innerHTML = '[ <a href="javascript:document.f1.submit()" class="cssLink3">Submit</a> ]';
    }
    else {
      obj.innerHTML = '[ ' + cnt + ' ]';
      cnt--;
      setTimeout('countdown(' + cnt + ')', 1000);
    }
  }
}

function delete_mail(type, id)
{
	if (!confirm("\nAre you sure you wish to delete this mail?")){
	}
	else
	{
		location.href="./account.php?mail=delete_mail&type=" + type + "&id=" + id;
	}
}

function preloadGlobal(){
	imgCheck=new Image(); 
	imgCheck.src=img_url + "images/check.gif";
	imgBang=new Image(); 
	imgBang.src=img_url + "images/bang2.gif";				
			
}

function nl2br(text){

text = escape(text);

if(text.indexOf('%0D%0A') > -1){

var re_nlchar = /%0D%0A/g ;

}else if(text.indexOf('%0A') > -1){

var re_nlchar = /%0A/g ;

}else if(text.indexOf('%0D') > -1){

var re_nlchar = /%0D/g ;

}

return unescape( text.replace(re_nlchar,'<br />') );

}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
function addGuest(){
	document.getElementById('add_guest_div').style.display='block';
}
function addComment(id){
	document.getElementById('add_comment_div').style.display='block';
	document.getElementById('blog_id').value=id;
}

function changePhoto(photo){
	document.getElementById('vendor_image').src=photo;
}

function gotoPage(frm){
location=
frm.p.
options[frm.p.selectedIndex].value
}

function getServices(region){
	if(region != ''){
		$j.ajax({
			type: 'GET',
			url: 'http://www.contemporarybride.com/handlers/services.php?region=' + region,
			success: function (response) {
				$j('#service_select').html(response);
			}
		});
		

		//ajax.update('http://www.contemporarybride.com/handlers/services.php?region=' + region, 'service_select');
		document.getElementById("services_tr").style.display='';
		document.getElementById("services_tr").style.paddingLeft='0';
		document.getElementById("submit_tr").style.display='';
	}else{
		document.getElementById("services_tr").style.display='none';
		document.getElementById("submit_tr").style.display='none';
		document.getElementById("service_select").innerHTML='';
	}
}
