
function openWindow(img,width,height) 
{
    openWindowWH(img,718,520) ;
}

var message = 'Полета с * са задължителни !';


function openWindowWH(img,width,height) 
{
    popupWin = window.open("/image/"+img,"Name","top=50,left=50,resizable=no,width="+width+",height="+height+",scrollbars=no,menubar=no");
	popupWin.focus();
}
function openMetricWindow(metric) 
{
    popupWin = window.open("metric.php?m="+metric,"Name","top=50,left=50,resizable=yes,width=500,height=300,scrollbars=yes,menubar=no");
	popupWin.focus();
}

function frmClearFields(){
	document.getElementById('cname').value='';
	document.getElementById('cfone').value='';
	document.getElementById('cmail').value='';
	document.getElementById('ctext').value='';
}

function SubmitForm(){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.forms["contactForm"].elements["cmail"].value;
	
	//if (reg.test(address) == false) {
	//	alert(document.getElementById('msgMissingMail').value);
	//}
	if (document.forms["contactForm"].elements["cname"].value.length < 1) {
		alert('Полето за име е задължително');
	} else if (document.forms["contactForm"].elements["ctext"].value.length < 1) {
		alert('Полето за текст е задължително');
	}
	else {
		document.forms['contactForm'].submit();
	}
}


var validateArr = new Array('id1', 'id2', 'id3', 'id4', 'id5');

//
function validate(objList){
	var isOverallValid=true;
	var res = "";
	
	for (var i=0; i<objList.length; i++){
		var id=objList[i];
		var elmType='unknown';
		var isElmValid= false;
		
		try{
			switch (document.getElementById(id).nodeName.toUpperCase()) {
				case 'INPUT':
					elmType = 'input';
					if(document.getElementById(id).value.length>0){
						isElmValid = true;
					}
					break;
				case 'SELECT':
					elmType = 'select' +document.getElementById(id).selectedIndex;
					if(document.getElementById(id).selectedIndex>-1){
						isElmValid = true;
					}
					break;
				default:
					isElmValid = false;
					break;
			}
		} catch(e){alert('document.getElementById('+id+').nodeName.toUpperCase() fails !\r\n Error name:' +e.name +'\r\n Error message:' +e.message)}
		
		try {document.getElementById(id+'_warn').className=(isElmValid)?'elmWarnOff':'elmWarnOn'; }
		catch (e){}
		
		isOverallValid = isOverallValid && isElmValid;
	}
	if(!isOverallValid){alert(message)};
	return isOverallValid;
}

function matchFields(matchFieldsMsg, objList){
	var isOverallValid = true;
	var matchValue = 'MATCH_FIELDS_DEFAULT';
	
	for (var i=0; i<objList.length; i++){
		var id=objList[i];
		
		try{
			switch (document.getElementById(id).nodeName.toUpperCase()) {
				case 'INPUT':
					if(matchValue == 'MATCH_FIELDS_DEFAULT') { matchValue = document.getElementById(id).value; }
					else if(matchValue != document.getElementById(id).value) { isOverallValid = false; }
					break;
				default:
					isOverallValid = false;
					break;
			}
		} catch(e){alert('document.getElementById('+id+').nodeName.toUpperCase() fails !\r\n Error name:' +e.name +'\r\n Error message:' +e.message)}
		
		if(!isOverallValid){break;}
	}
	
	if(!isOverallValid){alert(matchFieldsMsg)};
	return isOverallValid;
}

function navigateToURL(url){
	document.location=url;
}
function navigateToCatItem(idItem){
	// TODO - fix for the non correct pages
	document.location='/catalogue/item/'+idItem;
}

//this will add(if not in q string) or update variable in the query string and return the string
//the paramaeter wil be escaped
function QStr_SetVariable(vkey,vval){
    var url = "";
    var querystring = "";
    var isKeyInQString = 0;
    
    vkey= escape(vkey.toString());
    vval= escape(vval.toString());
    
    url= unescape(document.location+"");
    
    if (url.indexOf('?') >0) {
    	querystring= url.substring(url.indexOf('?')+1);
    	url= url.substring(0,url.indexOf('?'));
    	
    	var arr = querystring.split('&');

        // split out each name = value pair
	    for (var i=0;i<arr.length;i++)
	    {
            var pair = arr[i].split('=');
            if (pair[0] == vkey)
            {
                pair[1]= vval;
                isKeyInQString= 1;
                arr[i]=pair.join('=');
            }
        }
        
        querystring= arr.join('&');
        
        if(isKeyInQString) { url= url +"?" +querystring; }
    	else { url= url +"?" +querystring +'&' +vkey +'=' +vval; }
    }
    else{
        url= url +'?' +vkey +'=' +vval;
    }
    return url;
}

//this will get query string variable, if its not present will return null
//the vaule wil be unescaped
function QStr_GetVariable(vkey){
    var url = "";
    var querystring = "";
    var isKeyInQString = 0;
    
    vkey= escape(vkey.toString());
    var vval= null;
    
    url= unescape(document.location+"");
    
    if (url.indexOf('?') >0)
    {
    	querystring= url.substring(url.indexOf('?')+1);
    	
    	var arr = querystring.split('&');

        // split out each name = value pair
	    for (var i=0;i<arr.length;i++)
	    {
            var pair = arr[i].split('=');
            if (pair[0] == vkey) {
               vval= unescape(pair[1]);
               break;
            }
        }
    }
    return vval;
}
    
//set query string variable to selected from the drop down list
//this will reload the page, without submiting it
function DDL_ResetQStrValue(objSender, qStrKey){
    document.location= QStr_SetVariable(qStrKey,objSender.options[objSender.selectedIndex].value)
}
			
function blockNotDigits(e){
	var key;

	if(window.event) {
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 
	}
	else if(e.which) {
		// netscape
		key = e.which; 
	}
	else {
		// no event, so pass through
		return true;
	}

	var keychar = String.fromCharCode(key);
	var reg = /[0-4]/;
	return reg.test(keychar);
}


