/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}




/**********************************************************************************************************************************************
************************************************    DAJ DJELATNOST    *************************************************************************
***********************************************************************************************************************************************/
function showWidth(adr, adr1) {
    loadRS_ShowWidth(adr1+adr.value, 'loadShowWidth');
}
function loadShowWidth(rez) {
    var vrati;
    var dselect = document.getElementById('div_showWidth');
    var a = rez.split('%%');
    vrati = '<select id="width" name="width" onchange="showPrice(this, \'showPrice.php?ajax=on&amp;Del=\');" style="width : 205px;">';
    
    vrati = vrati+'<option value="">Choose width</option>';
    
    for (var i=0; i<a.length; i++) {
        var b = a[i].split('##');
        vrati = vrati+'<option value="'+b[0]+'-'+b[2]+'">'+b[1]+'</option>';
    }
    vrati = vrati+'</select>';
    dselect.innerHTML = vrati;

    
    
    
    
}
function loadRS_ShowWidth(url, func, action, tip) {
    var xmlhttp;
    getXmlObj(url);
    
    function getXmlObj(url) {
        if (window.XMLHttpRequest) {
              xmlhttp=new XMLHttpRequest()
          }else if (window.ActiveXObject){
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
          }
          if (xmlhttp) {
              xmlhttp.onreadystatechange=xmlhttpChange
              if (action) {
                  xmlhttp.open("POST", action, true);
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send(url);
              }else{
                  xmlhttp.open("GET",url,true);
                  xmlhttp.send(null);
              }
          }
      }
    function xmlhttpChange() {
        switch (xmlhttp.readyState) {
            case 0:
            case 1:
            case 2:
                //$('#pageLoader').jOverlay({opacity : 0.8});
                var vratiDole = '';
                vratiDole = vratiDole+'<input name="priceId" type="hidden" id="priceId" size="30" maxlength="32" value="">';
                vratiDole = vratiDole+'<input name="btnAddToCart" type="submit" id="btnAddToCart" value="Add to Cart" class="large blue awesome">';
                document.getElementById("div_showPrice").innerHTML = vratiDole;
                document.getElementById("div_loadWidth").innerHTML = "LOADING. PLEASE WAIT";
                break;
            case 3:
                //$.closeOverlay();
                document.getElementById("div_loadWidth").innerHTML = "";
                break;
            case 4:                
                //$.closeOverlay();
                document.getElementById("div_loadWidth").innerHTML = "";
                var vrati;
                if (tip == 'xml') {
                    vrati = xmlhttp.responseXML;
                }else{
                    vrati = xmlhttp.responseText;
                }
                if (xmlhttp.status==200){
                    eval(func+"(vrati);");
                }else{
                    //alert("Javio se problem prilikom dovlacenja podataka.\n"+xmlhttp.statusText);
                }
            break;
        }
    }
}




/**********************************************************************************************************************************************
************************************************    DAJ DJELATNOST    *************************************************************************
***********************************************************************************************************************************************/
function showPrice(adr, adr1) {
    loadRS_ShowPrice(adr1+adr.value, 'loadShowPrice');
}
function loadShowPrice(rez) {
    var vrati;
    var dselect = document.getElementById('div_showPrice');
    
    
    var a = rez.split('%%');
    
    vrati = '';
    for (var i=0; i<a.length; i++) 
    {
        var b = a[i].split('##');
        
        vrati = vrati+'<span class="PRICE">Price : '+b[1]+' &euro;</span>'; 
        vrati = vrati+'<div class="CLEAR" style="height: 30px;"></div>'; 
        vrati = vrati+'<input name="priceId" type="hidden" id="priceId" size="30" maxlength="32" value="'+b[0]+'">';
        vrati = vrati+'<input name="btnAddToCart" type="submit" id="btnAddToCart" value="Add to Cart" class="large blue awesome">'; 
        
    }
    
    dselect.innerHTML = vrati;
}
function loadRS_ShowPrice(url, func, action, tip) {
    var xmlhttp;
    getXmlObj(url);
    
    function getXmlObj(url) {
        if (window.XMLHttpRequest) {
              xmlhttp=new XMLHttpRequest()
          }else if (window.ActiveXObject){
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
          }
          if (xmlhttp) {
              xmlhttp.onreadystatechange=xmlhttpChange
              if (action) {
                  xmlhttp.open("POST", action, true);
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send(url);
              }else{
                  xmlhttp.open("GET",url,true);
                  xmlhttp.send(null);
              }
          }
      }
    function xmlhttpChange() {
        switch (xmlhttp.readyState) {
            case 0:
            case 1:
            case 2:
                //$('#pageLoader').jOverlay({opacity : 0.8});
                document.getElementById("div_loadPrice").innerHTML = "LOADING. PLEASE WAIT";
                break;
            case 3:
                //$.closeOverlay(); 
                document.getElementById("div_loadPrice").innerHTML = "";
                break;
            case 4:                
                //$.closeOverlay(); 
                document.getElementById("div_loadPrice").innerHTML = "";
                var vrati;
                if (tip == 'xml') {
                    vrati = xmlhttp.responseXML;
                }else{
                    vrati = xmlhttp.responseText;
                }
                if (xmlhttp.status==200){
                    eval(func+"(vrati);");
                }else{
                    //alert("Javio se problem prilikom dovlacenja podataka.\n"+xmlhttp.statusText);
                }
            break;
        }
    }
}



function checkAddToCart()
{
    with (window.document.frmAddToCart) 
    {
        productId.value  = productId.value;
        //priceId.value  = priceId.value;
        pack.value  = pack.value;
        width.value  = width.value;
        
        if (isEmpty(productId, 'Enter productId')) 
        {
            return false;
        } 
        else if (isEmpty(pack, 'You must choose pack type')) 
        {
            return false;
        }
        else if (isEmpty(width, 'You must choose width type')) 
        {
            return false;
        }
        else if (isEmpty(width, 'Enter width')) 
        {
            return false;
        }
        //else if (isEmpty(priceId, 'Enter priceId')) 
        //{
            //return false;
        //}
        else 
        {
            return true;
        }
    }
}
