var IsIE = navigator.appName.indexOf("Microsoft") != -1;

function cacheBuster(){
	var cb = new Date();
	cb = cb.getTime();
	return cb;
}

var aqu_types = new Array();
/* removed */
function make_aqu_array(){
	var http = new XMLHttpRequest();
	http.open('GET','/js/ajax.php?t=aquarray&ts=' + cacheBuster(), true);
	http.onreadystatechange = function(){
		if(http.readyState == 4 && http.status == 200){
			var html = http.responseText;
			//alert(html);
			aqu_type = html.split('<-->');
			var num = 0;
			//aqu_types[0] = 'select';
			for(var a=0;a<aqu_type.length;a++){
				aqu_types[aqu_types.length + 1] = aqu_type[a];
			}
		}
	}
	http.send(null);
}

function check_inquiry(){
	document.getElementById('sendInfo').value = 'Sending...';
	document.getElementById('sendInfo').disabled = true;
	var msg = 'Please Check the Following Fields \n';
	var flg = 0;
	
	if(document.getElementById('fname').value == ''){
		flg = 1;
		msg += 'First Name \n';
	}
	
	if(document.getElementById('lname').value == ''){
		flg = 1;
		msg += 'Last Name \n';
	}
	
	if(document.getElementById('phone').value == ''){
		flg = 1;
		msg += 'Phone Number \n';
	} else {
		// make sure all numbers or a + and numbers
	}
	
	if(document.getElementById('email').value == ''){
		flg = 1;
		msg += 'Email \n';
	} else {
		// email validator
	}
	
	if(document.getElementById('type').value == '0'){
		flg = 1;
		msg += 'Inquiry Type \n';
	}
	
	if(document.getElementById('description').value == ''){
		flg = 1;
		msg += 'Description \n';
	}
	
	if(flg == 1){
		alert(msg);
		document.getElementById('sendInfo').value = 'Submit Your Information';
		document.getElementById('sendInfo').disabled = false;
	} else {
		document.form1.submit();
	}
}

function doLogin(){
	document.loginfm.submit();
}

function doSubscribe(){
	document.subscribefm.submit();
}

function clearSubscribe(){
	document.getElementById('email').value = '';
}

function doReset(val){
	if(val == 'login'){
		document.getElementById('uname').value = '';
		document.getElementById('upass').value = '';
	}
}

function selectLaung(val){
	//alert(val);
	switch(val){
		case 'e':
			createCookie('laung','e',365,'lau');
			break;
		case 't':
			createCookie('laung','t',365,'lau');
			break;
	}
}

function createCookie(name,value,days,from) {
	var curCookie = readCookie('laung');
	var flg = 0;
	if(curCookie != value)
	{
		flg = 1;
	}
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		//alert('made it here, cookie is now set to ' + value);
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	if(flg == 1){
		window.location.reload();
	}
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function moveSort(dir,div,id,laung){
	var httpb = new XMLHttpRequest();
	httpb.open('GET','/js/ajax.php?t=navSort&dir=' + dir + '&id=' + id + '&laung=' + laung + '&ts=' + cacheBuster(), true);
	httpb.onreadystatechange = function(){
		if(httpb.readyState == 4 && httpb.status == 200){
			var html = httpb.responseText;
			//alert(html);
			var tbl = '<table width="475" border="1" cellpadding="2" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#CCCCCC">';
			tbl += '<tr>';
			tbl += '<td><div align="center">Name</div></td>';
			tbl += '<td width="41"><div align="center">Status</div></td>';
			tbl += '<td width="44"><div align="center">Order</div></td>';
			tbl += '<td width="68"><div align="center">Update</div></td>';
			tbl += '</tr>';
			tbl += html;	
			tbl += '</table>';
			if(IsIE){
				document.getElementById('nav_items').outerHTML = '<div id="nav_items">' + tbl + '</div>';
			} else {
				document.getElementById('nav_items').innerHTML = tbl;
			}
		}
	}
	httpb.send(null);
}

function changeNavStatus(status,div,id){
	var httpc = new XMLHttpRequest();
	httpc.open('GET','/js/ajax.php?t=navStatus&status=' + status + '&id=' + id + '&ts=' + cacheBuster(),true);
	httpc.onreadystatechange = function(){
		if(httpc.readyState == 4 && httpc.status == 200){
			var html = httpc.responseText;
			if(html == 'ok'){
				//alert(html);
				if(IsIE){
					if(status == '1'){
						document.getElementById('nav_status_' + div).outerHTML = '<div id="nav_status_' + div + '"><a href="javascript:;" onclick="changeNavStatus(\'2\',\'' + div + '\',\'' + id + '\')"><img src="active.png" alt="Click to make inactive" width="15" height="15" border="0" /></a></div>';
					} else {
						document.getElementById('nav_status_' + div).outerHTML = '<div id="nav_status_' + div + '"><a href="javascript:;" onclick="changeNavStatus(\'1\',\'' + div + '\',\'' + id + '\')"><img src="inactive.png" alt="Click to make active" width="15" height="15" border="0" /></a></div>';
					}
				} else {
					if(status == '1'){
						document.getElementById('nav_status_' + div).innerHTML = '<a href="javascript:;" onclick="changeNavStatus(\'2\',\'' + div + '\',\'' + id + '\')"><img src="active.png" id="nav_status_' + div + '" alt="Click to make inactive" width="15" height="15" border="0" /></a>';
					} else {
						document.getElementById('nav_status_' + div).innerHTML = '<a href="javascript:;" onclick="changeNavStatus(\'1\',\'' + div + '\',\'' + id + '\')"><img src="inactive.png" alt="Click to make active" width="15" height="15" border="0" /></a>';
					}
				}
			}
		}
	}
	httpc.send(null);
}

function doNavUpdate(div,id,laung)
{
	var httpd = new XMLHttpRequest();
	var txt = document.getElementById('nav_name_' + div).value;
	httpd.open('GET','/js/ajax.php?t=navUpdate&id=' + id + '&txt=' + txt + '&laung=' + laung + '&ts=' + cacheBuster(),true);
	httpd.onreadystatechange = function(){
		if(httpd.readyState == 4 && httpd.status == 200){
			var html = httpd.responseText;
			if(html == 'ok'){
				alert('Text has been updated!');
			} else {
				alert('Text HAS NOT BEEN UPDATED!');
			}
		}
	}
	httpd.send(null);
}

function doConUpdate(div,id){
	var httpe = new XMLHttpRequest();
	var thai = document.getElementById('thai_val_' + div).value;
	var eng = document.getElementById('eng_val_' + div).value;
	//alert(thai + ' : ' + eng);
	httpe.open('GET','/js/ajax.php?t=updatecons&id=' + id + '&thai=' + thai + '&eng=' + eng + '&ts=' + cacheBuster(),true);
	httpe.onreadystatechange = function(){
		if(httpe.readyState == 4 && httpe.status == 200){
			var html = httpe.responseText;
			alert(html);
			/*
			if(html == 'ok'){
				alert('Text has been updated!');
			} else {
				alert('Text HAS NOT BEEN UPDATED!');
			}
			*/
		}
	}
	httpe.send(null);
}

function showLeadActions(id){
	window.open('viewactions.php?id=' + id,'actionswindow','width=800,height=500'); 
}

function startQuote(cid,uid,sess){
	window.open('../office_templates/quote.php?type=make&id=' + cid + '&uid=' + uid + '&sess=' + sess);
}

function startInvoice(cid,uid,sess){
	window.open('../office_templates/invoice.php?step=1&cid=' + cid + '&uid=' + uid + '&sess=' + sess);
}


function viewFullImage(path){

	//var largePath = path.replace(/_tn.jpg/gi,".jpg");
	var largePath = path;
	window.open("../pic.php?path=" + path ,"PicWin","toolbar=no,status=no,resizable=no,scrollbars=no,menubar=no");
}

function showVideo(path,ext){
	window.open("../vid.php?path=" + path + "&ext=" + ext,"VidWin","toolbar=no,status=no,resizable=no,scrollbars=no,menubar=no");
}

function view_selected_quote(uid,sess,ccid){
	var qid = document.getElementById('ci_list').value;
	window.open('../office_templates/quote_builder.php?view=' + qid + '&print=1');
}

function showVideoPort(flv,size)
{
	window.open('../portfolio/videos.php?v=' + flv + '&s=' + size,'vidwin','width=470,height=365');
}