function normalizeExpression(exp) {
	var str = exp.replace(/&lt;/gi, "<");
	str = str.replace(/&gt;/gi, ">");
	str = str.replace(/&amp;/gi, "&");
	str = str.replace(/&quot;/gi, "\"");
	
	
	return str;
}

function isNumber(data) {
	if (trim(data) != '' && (Number(data).toString() != 'NaN')) {
		return true;
	} else {
		return false;
	}
}

function showHideItems(shID, flag) {

	// alert(shID);
	if (document.getElementById(shID) != null) {
		if (document.getElementById(shID) && flag == 'show') {
			document.getElementById(shID).style.display = 'inline';
		} else if (document.getElementById(shID) && flag == 'hide') {
			document.getElementById(shID).style.display = 'none';
		}
	}
}

function evaluateAutoQ(exp, qty) {

	var str = exp.replace(/Q/g, qty);
	str = normalizeExpression(str);
	var result = eval(str);
	return result;
}

/** 
 * @param serviceID
 * @param optSet
 * @param optCode
 * @return new replaced service ID
 */
function replaceOptionCode(serviceID, optSet, optCode) {
	var arrId = serviceID.split("-");
	var arrOptSet = optSet.split("-");
	var found = false;
	for (i=0; i< arrId.length; i++) {
		for (j=0; j< arrOptSet.length; j++) {
			if (arrId[i] == arrOptSet[j]) {
				arrId[i] = optCode;
				found = true;
				break;
			}
		}		
		
		if (found) {
			break;
		}
	}
	
	var result = "";
	
	if (found) {
		for (i=0; i< arrId.length; i++) {
			if (result != "") {
				result = result + "-";
			}
			result = result + arrId[i];
		}
	}
	return result;
}

/**
 * Replaces all instances of the given substring.
 */
String.prototype.replaceAll = function(
		strTarget, // The substring you want to replace
		strSubString // The string you want to replace in.
	){
	var strText = this;
	
	if (strTarget != '' && strSubString != '') {
		
		var intIndexOfMatch = strText.indexOf( strTarget );
		 
		// Keep looping while an instance of the target string
		// still exists in the string.
		while (intIndexOfMatch != -1){
			// Replace out the current instance.
			strText = strText.replace( strTarget, strSubString )
			 
			// Get the index of any next matching substring.
			intIndexOfMatch = strText.indexOf( strTarget );
		}
		
	}
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}

/**
 * Convert a Id list with format "[?,?]" to array data
 */
function convertListIds(str) {
    var result = new Array();
   
    if (str!=null) {
	    var tmp = str.replace("[", "");
	    tmp = tmp.replace("]", "");
	    tmp = tmp.replace(" ", "");
	   
	    
	    if (tmp.length > 0) {    	
	    	var arr = tmp.split(",");
	    	for (i=0; i<arr.length; i++) {
	    		result[i] = trim(arr[i]);
	    	}
	    }
    }
    
    return result;
}

function formatNumber(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function setOption(objOpt){

	disOptions = new Array();
	
	var radios = document.getElementsByTagName('input');	
	
	changeOption(objOpt);

	//alert('disOptions ' + disOptions);
	var inOptions = validateDisOption();

	//alert('inOptions ' + inOptions);
	while (inOptions.length != 0) {
		//changeOption(objOpt);
		
		for (var j=0; j<inOptions.length; j++) {

   			var setSelected = false;
    		for (var i=0; i<radios.length; i++) {
		       	var input = radios[ i ];
		       	if ((input.type == 'radio' || input.type == 'checkbox') && inOptions[j] == input.alt ) {
		       		if (findDisOption(input.value) ) {
		       			input.checked = false;
		       		} else {
		       			if (!setSelected) input.checked = true;
			       		setSelected = true;
		       		
		       		}
		       	}
    		}       	 
		}

   		changeOption(objOpt);
   		inOptions = validateDisOption();
	}

	//alert('toto');

	for (var i=0; i<radios.length; i++) {
       	var input = radios[ i ];
       	if ((input.type == 'radio' || input.type == 'checkbox') ) {
           	
       		if (findDisOption(input.value)){
           		input.disabled = true;
           		
       		} else {
       			input.disabled = false;
       		}
       	} 
	}

	//alert('disOptions ' + disOptions);
	
}

function validateDisOption() {
	var radios = document.getElementsByTagName('input');	

	var result = new Array();	
	for (var i=0; i<radios.length; i++) {
       	var input = radios[i];
       	if ((input.type == 'radio' || input.type == 'checkbox') ) {
       		if (findDisOption(input.value)&& input.checked) {
           		//need to change default option
       			result.push(input.alt);
       		}	        
       	} 
	}

	return result;
}

function addDisOption(optionCode){
	if (disOptions == null) disOptions = new Array();

	for (var i=0; i< disOptions.length; i++) {
		if (disOptions[i] == optionCode) {
			
			 return;
		}
	}
	disOptions.push(optionCode);
}


function findDisOption(optionCode){
	if (disOptions == null) disOptions = new Array();

	for (var i=0; i< disOptions.length; i++) {
		if (disOptions[i] == optionCode) {
			
			 return true;
		}
	}
	return false;
}
function changeOption(objOpt) {
	
	var sku = getComSku();

	//get option index
	
	var currentOpt = '';
	var arr = sku.split('-');

	if (objOpt.checked) {
		currentOpt = objOpt.value;
	}

	
	var tmpArr = new Array();
	var tmpCode = '';
	for (var i =0; i<arr.length;i++) {
		if (arr[i] == currentOpt) {		
			tmpArr.push(arr[i] );
			break;
		}
		else {
			tmpArr.push(arr[i] );
		}
	}
	
	//disable unavailable option
	for (var i=0; i<tmpArr.length; i++) {
		if (tmpCode != '' ) tmpCode = tmpCode + '-';
		tmpCode = tmpCode + tmpArr[i];
		//alert('last option: ' + tmpCode);
	 	getDisOpt(tmpCode, sku);		
	}

	//alert(disGroup);
}

function getDisOpt(startOpt, sku) {
	for(var j=0; j< comPrice.length; j++){
		if (comPrice[j].price == "" && comPrice[j].priceFormula == "")  {
			var comSKU = comPrice[j].combinationCode;
			
			if (comSKU.indexOf(startOpt) == 0) {
				var comSKU = comSKU.substring(startOpt.length); 
				var restArr = comSKU.split("-");
				//alert(comSKU)
				if (restArr.length == 2) addDisOption(restArr[1]);
				else {
					var k=1;
					if (restArr.length > 2) {
						for (; k<restArr.length; k++) {
							if (!checkServiceId(sku, restArr[k])) {
								break;
								
							}	
						} 
					}
					if ((k + 1) == restArr.length) {
						addDisOption(restArr[k]);
					}
				}
			}		
		}
	}
}

//JavaScript Document
<!-- 
function ltrim(str) //Left trim
{ 
	return str.replace(/^[ ]+/, ''); 
} 

function rtrim(str) //Right trim
{ 
	return str.replace(/[ ]+$/, ''); 
} 

function trim(str) //Trim function
{ 
	return ltrim(rtrim(str)); 
} 
function max_chars(str,ctr) //Returns true only if str has less than ctr number of characters.
{ 
	if(str.length<ctr)
	{
	return false;
	}
	return true;
} 
function validate_mail(str)/*E-mail validation in javascript*/
{
	var re = new RegExp(/^[\w\.\-]+\@[\w\.\-]+\.[a-z\.]{2,6}$/);
	if(str.search(re)==-1)
	{
	return false;
	}
	return true;
}

function validate_string(str)/*Allows only alphabets and singlequotes in a string*/
{
		var re = new RegExp(/^[a-zA-Z0-9']+$/);
		if(str.search(re)==-1)
		{
		return false;
		}
		return true;
}
function validate_date(day,mon,year)	//Date validation
{
	mon=mon-1;	//Javascript months are in the range 0 to 11
	dteDate=new Date(year,mon,day);//Creating a date object
	return ((day==dteDate.getDate()) && (mon==dteDate.getMonth()) && (year==dteDate.getFullYear()));// Validates the date
}
function beg_end(str)	//Returns false if the beginning or end characters of str is a white space.
{
	if(str.charAt(0)==' '||str.charAt(str.length-1)==' ')
		{
			return false;
		}
	return true;	
}
//-->
