
function go(loc){
	window.location=loc;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


//Function til að taka út item í Orders

function checkRmItems(){

	var totalitems = document.invoice_update.length;
	var ccount = 0;
	for(i = 0; i <= totalitems; i++){
		if(document.invoice_update.elements[i] != undefined && document.invoice_update.elements[i].type == "checkbox"){
			var box = document.invoice_update.elements[i];
			if(box.checked == true){
				return true;
			}
		}
	}
}

function postRemoveOrderItem(){
	return document.invoice_update.submit();
}

function openfilteroptions(el,id){
	var obj = document.getElementById('PopDiv');
	var myloc = document.getElementById(el);
	my_x = findPosX(myloc);
	my_y = findPosY(myloc);
	obj.style.left = my_x;
	obj.style.top = my_y + 15;
	obj.style.display = "";
	document.getElementById('OrderID').value = id;
	document.getElementById('order_quant').value = "";
}

function openfilterorderoptions(el,id){
	var obj = document.getElementById('OrderOnly');
	var myloc = document.getElementById(el);
	my_x = findPosX(myloc);
	my_y = findPosY(myloc);
	obj.style.left = my_x + 3;
	obj.style.top = my_y + 23;
	obj.style.display = "";
	document.getElementById('OrderID').value = id;
	document.getElementById('order_only_quant').value = "";
}

function goTecnicalInfo(){
	var id = document.getElementById('OrderID').value;
	window.location='?tecnicalinfo='+id;
}

var over;
over = true;

function killDiv(){
	over = true;
	var obj = document.getElementById('PopDiv');
	var obj2 = document.getElementById('OrderOnly');
	obj.style.display = "none";
	obj2.style.display = "none";
}

function swapDisplay(elementID){
	var obj = document.getElementById(elementID);
	obj.style.display = (obj.style.display == "" ? "none" : "");
}

function submitOrder(){
	var quant = document.getElementById('order_quant');
	var itemID = document.getElementById('OrderID');
	var min = document.getElementById('MinimumOrder');

	//if(quant.value < 12){
	if(quant.value < 1){
		min.style.display = '';
		holdKillDiv();
		return false;
	}else{
		min.style.display = 'none';
		xmlhttpPost('invoice_ajax.php?id='+itemID.value+'&quant='+quant.value);
		killDiv();
	}

}
function submitOrderOnly(){
	var quant = document.getElementById('order_only_quant');
	var itemID = document.getElementById('OrderID');
	var min = document.getElementById('MinimumOrder');
	//if(quant.value < 12){
	if(quant.value < 1){
		min.style.display = '';
		holdKillDiv();
		return false;
	}else{
		min.style.display = 'none';
		xmlhttpPost('invoice_ajax.php?id='+itemID.value+'&quant='+quant.value);
		killDiv();
	}
}

function updateInvoice(){
	xmlhttpPost('invoice_ajax.php');
}

function resetDiv(){
	var divobj = document.getElementById('PopDiv');
}



function intKillDiv(){
	over = false;
}
function holdKillDiv(){
	over = true;
}

// AJAX
function xmlhttpPost(strURL) {
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
           updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(strURL);
}

function updatepage(vars){

	splitStr = vars.split("\n");
	var out = "";
	var bclass = "a";
	var c = 0;
	var quantTotal = 0;
	var priceTotal = 0;
	out = "<table width='150' border='0' cellpadding='2' cellspacing='0' align='center' class='InvoiceList'>\n";
	out += "<tr class='Header'><td>&nbsp;</td><td title='World Of Filters'>WOF</td><td title='Quantity'>Qnt.</td><td title='Item price'>Price</td></tr>\n";
	for(i = 0; i < splitStr.length; i++){
		itemSplit = splitStr[i].split(":");
		if(itemSplit[0] != ""){
			c++;
			bclass = (c % 2 ? "a" : "b");
			out += "<tr class='Item_"+bclass+"' onMouseOver=\"this.className='ItemOver';\" onMouseOut=\"this.className='Item_"+bclass+"';\">\n";
			out += "<td><span class='rm' onClick=\"removeFromInvoicve('"+itemSplit[0]+"');\" onMouseOver=\"this.className='rmOver';\" onMouseOut=\"this.className='rm';\" title='Remove item :"+itemSplit[0]+"'>X</span></td>\n";
			out += "<td>" + itemSplit[0] + "</td>\n";
			out += "<td>" + itemSplit[1] + "</td>\n";
			if(itemSplit[2] != "Login"){
				out += "<td>€ " + itemSplit[2] + "</td>\n";
			}else{
				out += "<td><a href='?pageid=7'>Login</a></td>\n";
			}
			out += "</tr>\n";
			// Leggur saman þar sem er í Invoice
			quantTotal += parseInt(itemSplit[1]);
			priceTotal += parseFloat(itemSplit[2] * itemSplit[1]);
		}
	}
	if(splitStr.length >= 1 ){
		out += "<tr class='Header' style='background:#FFFFFF;'><td>&nbsp;</td><td>Total</td><td>";
		out += quantTotal;
		out += "</td><td>€ ";
		out += (isNaN(priceTotal)  ? "0" : Math.round(priceTotal * 100) / 100);
		out += "</td></tr>\n";
	}
	out += "</table>\n";

	var invoiceDiv = document.getElementById('Invoice');
	invoiceDiv.innerHTML = out;

}

function removeFromInvoicve(id){
	xmlhttpPost('invoice_ajax.php?id='+id+'&rm=true');
}

function checkForm(form){
	if(form == 'CrossReferance'){
		var obj = document.getElementById('crossreferance');
		if(obj.value == ""){
			obj.focus();
			alert("Value must be set in field");
			return false;
		}
	}
	if(form == 'Tecnical'){
		var obj = document.getElementById('tinfo');
		if(obj.value == ""){
			obj.focus();
			alert("Value must be set in field");
			return false;
		}
	}
	return true;
}

function checkRegistrationForm(){

	var company_name = document.getElementById('company_name');
	var company_address = document.getElementById('company_address');
	var company_city = document.getElementById('company_address');
	var company_state_country = document.getElementById('company_state_country');
//	var company_zip = document.getElementById('company_zip');
	var contact_name = document.getElementById('contact_name');
	var contact_tel = document.getElementById('contact_tel');
	var contact_email = document.getElementById('contact_email');
	var contact_password = document.getElementById('contact_password');
	var contact_password_confirm = document.getElementById('contact_password_confirm');
	var terms = document.getElementById('terms');
	
	var error = "";

	if(company_name.value == ""){
		error += "Company name must be set\n";
	}
	if(company_address.value == ""){
		error += "Company address must be set\n";
	}
	if(company_city.value == ""){
		error += "Company city must be set\n";
	}
	if(company_state_country.value == ""){
		error += "Company state/country must be set\n";
	}
	/*
	if(company_zip.value == ""){
		error += "Company zip must be set\n";
	}
	*/
	if(contact_name.value == ""){
		error += "Contact name must be set\n";
	}
	if(contact_tel.value == ""){
		error += "Telephone must be set\n";
	}
	if(contact_email.value == ""){
		error += "Email must be set\n";
	}
	if(contact_email.value == ""){
		error += "Email must be set\n";
	}
	if(contact_email.value.indexOf('@') == -1){
		error += "Email must be an email\n";
	}
	
	if(contact_password.value == ""){
		error += "Password must be set\n";
	}
	if(contact_password_confirm.value == ""){
		error += "Confirm password must be set\n";
	}
	if(contact_password_confirm.value !== contact_password.value){
		error += "Password and Confirm password mus be same\n";
	}

	if(terms.checked == false){
		error += "You must check the Terms and Conditions to continue\n";
	}

	if(error != ""){
		alert(error);
		return false;
	}else{
		return true;
	}
}

function checkComfirmationForm(){

	var company_name = document.getElementById('company_name');
	var company_address = document.getElementById('company_address');
	var company_city = document.getElementById('company_address');
	var company_state_country = document.getElementById('company_state_country');
	var company_zip = document.getElementById('company_zip');
	var contact_name = document.getElementById('contact_name');
	var contact_tel = document.getElementById('contact_tel');
	var contact_email = document.getElementById('contact_email');
	var wof_password = document.getElementById('wof_confirm_password');
	var wof_password_confirm = document.getElementById('wof_confirm_password_confirm');

	var error = "";

	if(company_name.value == ""){
		error += "Company name must be set\n";
	}
	if(company_address.value == ""){
		error += "Company address must be set\n";
	}
	if(company_city.value == ""){
		error += "Company city must be set\n";
	}
	if(company_state_country.value == ""){
		error += "Company state/country must be set\n";
	}
	if(company_zip.value == ""){
		error += "Company zip must be set\n";
	}
	if(contact_name.value == ""){
		error += "Contact name must be set\n";
	}
	if(contact_tel.value == ""){
		error += "Telephone must be set\n";
	}
	if(contact_email.value == ""){
		error += "Email must be set\n";
	}
	if(contact_email.value == ""){
		error += "Email must be set\n";
	}
	if(contact_email.value.indexOf('@') == -1){
		error += "Email must be an email\n";
	}
	if(wof_password.value == ""){
		error += "Password must be set\n";
	}

	if(wof_password.value !== wof_password_confirm.value){
		error += "Passwords must match";
	}

	if(error != ""){
		alert(error);
		return false;
	}else{
		return true;
	}
}

function checkLoginForm(){

	var username = document.getElementById('wof_username');
	var password = document.getElementById('wof_password');

	var error = "";

	if(username.value == "" || username.value == "Username"){
		error += "Username must be a email\n";
	}
	if(password.value == "" || password.value == "password"){
		error += "Password must be a valid password\n";
	}
	if(error != ""){
		alert(error);
		return false;
	}else{
		return true;
	}

}

function CheckEnter(evt) {
    var keyCode = document.layers?evt.which:evt.keyCode;
    if (keyCode == 13) {
        return true;
    }
    return false;
}

function setStyle(){
	var styleID = document.getElementById('WebWraper');
	var styleIDMain = document.getElementById('Display').offsetHeight;
	//alert(styleIDMain.offsetHeight);
	if(styleIDMain < document.body.clientHeight){
		resizeValue = document.body.clientHeight;
	}else{
		resizeValue = (styleIDMain + 200);
	}
	var winValue = resizeValue;
	return styleID.style.height = winValue+"px";
}


function setElementStyle(obj1,obj2){
	var item = document.getElementById(obj1);
	var item_2 = document.getElementById(obj2);
	var Eheight = item.offsetHeight
	return item_2.height = Eheight;
}


function swapDisplay(item_1,item_2){
	var obj_1 = document.getElementById(item_1);
	var obj_2 = document.getElementById(item_2);

	var tab_1 = document.getElementById('ApplicationButton');
	var tab_2 = document.getElementById('CrossRefButton');

	if(obj_1.style.display == 'none'){
		obj_1.style.display = '';
		obj_2.style.display = 'none';
	}

	if(item_1 == "AppDisplay"){
		tab_1.className='Selected';
		tab_2.className='Button';
	}
	if(item_1 == "CrossDisplay"){
		tab_1.className='Button';
		tab_2.className='Selected';
	}

	setStyle();
}

//Functino sem tékkar á hvort að það séu komin 4 stafinn í inputfieldið og færir viðkomandi í næsta hólf ef svo er.

function checkQuickFilterId(){
	var inputfield = document.getElementById('quickorder_filter_id').value;
	if(isNaN(inputfield)){
			alert("World of filters number has to be numeric");
			document.getElementById('quickorder_filter_id').value = "";
			return false;
		}
	if(inputfield.length >= 4){
		document.getElementById('quickorder_quant').focus();
	}
}

//Function sem tékkar á því hvort að það filter_id sem sett var í Quick order hafi verið til í grunni og að magnið sé ekki 0.

function validateQuickOrder(){
	var filter_id = document.getElementById('quickorder_filter_id').value;
	var quant = document.getElementById('quickorder_quant').value;
	//Tékkar á því hvort að quant sé tölustafur
	if(isNaN(quant)){
		alert("Quantity has to be numeric");
		return false;
	}
	//Tékkar á því hvort að quant sé núll
	if(quant == 0){
		alert("Quantity cannot be 0 (zero)");
		return false;
	}
	/*
	if(quant < 12){
		alert("Quantity cannot be less than 12 pc");
		document.getElementById('quickorder_quant').value = 12;
		return false;
	}
	*/
	//AJAX til að tékka á hvort að talan sem er verið setja inn sé gild.
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', 'check_wofnr.php?wof_id='+filter_id, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
         // alert("í lagi");
	         if(self.xmlHttpReq.responseText == 1){
	         //Póstar inn filter_id og magni inní Session
	         	xmlhttpPost('invoice_ajax.php?id='+filter_id+'&quant='+quant);
	         	//ef það er lagi þá hreinsast út formið.
	        	document.getElementById('quickorder_filter_id').value = "";
				quant = document.getElementById('quickorder_quant').value = "";
				document.getElementById('quickorder_filter_id').focus();
	         }else{
	         	alert("WOF nr. Not valid");
	         	document.getElementById('quickorder_filter_id').focus();
	         }
        }
    }
    self.xmlHttpReq.send("check_wofnr.php?wof_id="+filter_id);

}

function checkSendByEmail(){

	var name = document.getElementById('sender_name');
	var email = document.getElementById('send_to_email');
	var error = "";
	if(name.value == ""){
		error += "Please set Sender name\n";
	}
	if(email.value == ""){
		error += "Please set email\n";
	}
	if(email.value.indexOf('@') == -1){
		error += "Please set valid email\n";
	}

	if(error != ""){
		alert(error);
		return false;
	}else{
		return true;
	}



}
