/*
------------------------------------------------------------------------------------------------------------------------------------------------
COL Contact form functions
------------------------------------------------------------------------------------------------------------------------------------------------	
*/

function populatelist(elementid,kind,itemid)
{
	var colcode = document.getElementById('companycode').value;
	var colurl = 'colcontact.php?gotourl='+document.getElementById('colurl').value+'web/svc.php';

	var getinfo = 'svc_id=contactform&showxmlheader=no';
	var postinfo = 'companycode='+colcode+'&cf_action=getlist&cf_kind='+kind+'&cf_itemid='+itemid+'&cf_elementid='+elementid;
	var partnerObj = document.getElementById('usr_franchisor');
	var partnerid = '';
	if (partnerObj) partnerid = partnerObj.value;
	if (partnerid!=null && partnerid!='') postinfo+='&usr_franchisor='+partnerid;
	ajaxReq(colurl+'&'+getinfo,postinfo,'populatelist_data');	
}


function populatelist_data(response)
{
   	var xmldoc = xmlFix(response);
   	var	xmldata = xml2array(xmldoc);  
   	   		
   	if (!xmldata['colweb']) return;
   	   		 
   	if (xmldata['colweb']['listinfo']['cf_elementid'])
   	{
   		obj = document.getElementById(xmldata['colweb']['listinfo']['cf_elementid']);
  		if (!obj) return; 
   	}  
   	
   	if (xmldata['colweb']['list'])
	   	if (xmldata['colweb']['list']['item'])
	   	{     
	   		var list = xmldata['colweb']['list']['item'];
	   		   		
	   		for (i in list)
	   		{
	   			var opt = document.createElement("OPTION");
	   			opt.text = list[i]['title']; 
	   			opt.value = list[i]['value'];
	   			if (opt.text=="undefined") opt.text="";
				obj.options.add(opt);
	   		}   	
	   	}   	
}





function change_companycode_byzip(formname,zipcode)
{
	zipcode = zipcode.toUpperCase();
	zipcode = zipcode.slice(0,3);
	if (!zipcode || zipcode+''=='') return;
	
	switch (zipcode+"")
	{
		case "K0A":
		case "K0J":
			 comp = 'KKOttawa';		
		break;		
		case "L0L":
		case "L0M":
		case "L4M":
		case "L4N":
		case "L4R":
		case "L9M":
		case "L9S":
		case "L9Y":
		case "L9Z":
		case "N4K":
		case "N4L":
		case "N4N":
			 comp = 'KKBarrie';		
		break;		
		case "L0P":
		case "L6H":
		case "L6J":
		case "L6K":
		case "L6L":
		case "L6M":
		case "L7L":
		case "L7M":
		case "L7N":
		case "L7P":
		case "L7R":
		case "L7S":
		case "L7T":
		case "L7V":
		case "L9T":
			 comp = 'KKBurOak';		
		break;		
		case "T0K":
		case "T0L":
		case "T1A":
		case "T1B":
		case "T1C":
		case "T1G":
		case "T1H":
		case "T1J":
		case "T1K":
		case "T1M":
		case "T1R":
		case "T1S":
		case "T1V":
		case "T2C":
		case "T2H":
		case "T2J":
		case "T2W":
		case "T2X":
		case "T2Y":
		case "T2Z":
		case "T3M":
			 comp = 'KKCalgary South';		
		break;		
		case "T1L":
		case "T1W":
		case "T2K":
		case "T2L":
		case "T2M":
		case "T2N":
		case "T2P":
		case "T2R":
		case "T2S":
		case "T2T":
		case "T3A":
		case "T3B":
		case "T3C":
		case "T3E":
		case "T3G":
		case "T3H":
		case "T3K":
		case "T3L":
		case "T3P":
		case "T3R":
		case "T3Z":
		case "T4A":
		case "T4B":
		case "T4C":
			 comp = 'KKCalgary West';		
		break;		
		case "T5A":
		case "T5B":
		case "T5C":
		case "T5E":
		case "T5G":
		case "T5H":
		case "T5J":
		case "T5K":
		case "T5L":
		case "T5M":
		case "T5N":
		case "T5P":
		case "T5R":
		case "T5S":
		case "T5T":
		case "T5V":
		case "T5W":
		case "T5X":
		case "T5Y":
		case "T5Z":
		case "T6A":
		case "T6B":
		case "T6C":
		case "T6E":
		case "T6G":
		case "T6H":
		case "T6J":
		case "T6K":
		case "T6L":
		case "T6M":
		case "T6N":
		case "T6P":
		case "T6R":
		case "T6S":
		case "T6T":
		case "T6V":
		case "T6W":
		case "T6X":
		case "T8A":
		case "T8B":
		case "T8C":
		case "T8E":
		case "T8G":
		case "T8H":
			 comp = 'KKEdmonton South';		
		break;		
		case "L3T":
		case "L4B":
		case "L4C":
		case "L4E":
		case "L4J":
		case "L4K":
		case "L4S":
		case "L4W":
		case "L4X":
		case "L4Y":
		case "L4Z":
		case "L5A":
		case "L5B":
		case "L5C":
		case "L5E":
		case "L5G":
		case "L5H":
		case "L5J":
		case "L5K":
		case "L5L":
		case "L5M":
		case "L5N":
		case "L5P":
		case "L5R":
		case "L5S":
		case "L5T":
		case "L5V":
		case "L5W":
		case "L6P":
		case "L6R":
		case "L6S":
		case "L6T":
		case "L6V":
		case "L6W":
		case "L6X":
		case "L6Y":
		case "L6Z":
		case "L7A":
		case "L7C":
		case "L7E":
		case "L7G":
		case "L7J":
		case "L7K":
		case "L9R":
		case "L9V":
		case "L9W":
		case "M2M":
		case "M2N":
		case "M2R":
		case "M3H":
		case "M3J":
		case "M3K":
		case "M3L":
		case "M3M":
		case "M3N":
		case "M4A":
		case "M4B":
		case "M4N":
		case "M4R":
		case "M4V":
		case "M5M":
		case "M5N":
		case "M5P":
		case "M5R":
		case "M6A":
		case "M6B":
		case "M6C":
		case "M6E":
		case "M6G":
		case "M6H":
		case "M6J":
		case "M6K":
		case "M6L":
		case "M6M":
		case "M6N":
		case "M6P":
		case "M6R":
		case "M6S":
		case "M7A":
		case "M7Y":
		case "M8V":
		case "M8W":
		case "M8X":
		case "M8Y":
		case "M8Z":
		case "M9A":
		case "M9B":
		case "M9C":
			 comp = 'KKFI - Julia';		
		break;		
		case "L2A":
		case "L2E":
		case "L2E":
		case "L2G":
		case "L2H":
		case "L2J":
		case "L2M":
		case "L2N":
		case "L2P":
		case "L2R":
		case "L2S":
		case "L2V":
		case "L2W":
		case "L3B":
		case "L3C":
		case "L3K":
		case "L3M":
		case "L8E":
		case "L8G":
		case "L8H":
		case "L8J":
		case "L8K":
		case "L8L":
		case "L8M":
		case "L8N":
		case "L8P":
		case "L8R":
		case "L8S":
		case "L8T":
		case "L8V":
		case "L8W":
		case "L9A":
		case "L9B":
		case "L9C":
		case "L9G":
		case "L9H":
		case "L9K":
		case "LST":
		case "N1A":
		case "N1C":
		case "N1E":
		case "N1G":
		case "N1H":
		case "N1K":
		case "N1L":
		case "N1M":
		case "N1P":
		case "N1R":
		case "N1S":
		case "N1T":
		case "N2A":
		case "N2B":
		case "N2C":
		case "N2E":
		case "N2G":
		case "N2H":
		case "N2J":
		case "N2K":
		case "N2L":
		case "N2M":
		case "N2N":
		case "N2P":
		case "N2R":
		case "N2T":
		case "N2V":
		case "N3A":
		case "N3B":
		case "N3C":
		case "N3E":
		case "N3H":
		case "N3L":
		case "N3P":
		case "N3R":
		case "N3S":
		case "N3T":
		case "N3V":
		case "N3W":
		case "N3Y":
		case "N4B":
		case "N4G":
		case "N4S":
		case "N4T":
		case "N4V":
		case "N4X":
		case "N4Z":
		case "N5A":
		case "N5C":
		case "N5H":
		case "N5L":
		case "N5P":
		case "N5R":
		case "N5V":
		case "N5W":
		case "N5X":
		case "N5Y":
		case "N5Z":
		case "N6A":
		case "N6B":
		case "N6C":
		case "N6E":
		case "N6G":
		case "N6H":
		case "N6J":
		case "N6K":
		case "N6L":
		case "N6M":
		case "N6N":
		case "N6P":
		case "N7A":
			 comp = 'KKFI - Shaun';		
		break;		
		case "T0J":
		case "T0M":
		case "T1P":
		case "T1X":
		case "T1Y":
		case "T2A":
		case "T2B":
		case "T2E":
		case "T2G":
		case "T2V":
		case "T3J":
		case "T3N":
		case "T4G":
		case "T4H":
		case "T4N":
		case "T4P":
		case "T4R":
		case "T4S":
			 comp = 'KKFI - Tom';		
		break;		
		case "K0L":
		case "K0M":
		case "K7A":
		case "K7G":
		case "K7K":
		case "K7L":
		case "K7M":
		case "K7N":
		case "K7P":
		case "K7R":
		case "K8N":
		case "K8P":
		case "K8R":
		case "K8V":
		case "K9A":
		case "K9H":
		case "K9J":
		case "K9K":
		case "K9L":
		case "K9V":
		case "L0A":
		case "L1A":
		case "L3V":
			 comp = 'KKKawartha Lakes';		
		break;		
		case "L3P":
		case "L3R":
		case "L3S":
		case "L6B":
		case "L6C":
		case "L6E":
		case "L6G":
		case "M1B":
		case "M1G":
		case "M1H":
		case "M1P":
		case "M1R":
		case "M1S":
		case "M1T":
		case "M1V":
		case "M1W":
		case "M1X":
		case "M2H":
		case "M2J":
		case "M2K":
		case "M3A":
		comp='KKMarkham';		
		break;		
		case "K1A":
		case "K1B":
		case "K1C":
		case "K1E":
		case "K1G":
		case "K1H":
		case "K1J":
		case "K1K":
		case "K1L":
		case "K1M":
		case "K1N":
		case "K1P":
		case "K1R":
		case "K1S":
		case "K1T":
		case "K1V":
		case "K1W":
		case "K1X":
		case "K1Y":
		case "K1Z":
		case "K2A":
		case "K2B":
		case "K2C":
		case "K2E":
		case "K2G":
		case "K2H":
		case "K2J":
		case "K2K":
		case "K2L":
		case "K2M":
		case "K2P":
		case "K2R":
		case "K2S":
		case "K2T":
		case "K2V":
		case "K2W":
		case "K4A":
		case "K4B":
		case "K4C":
		case "K4K":
		case "K4M":
		case "K4P":
		case "K4R":
		case "K6A":
		case "K7C":
		case "K7H":
		case "K7S":
		case "K7V":
		case "K8A":
		case "K8B":
		case "K8H":
			 comp = 'KKOttawa';		
		break;		
		case "M1C":
		case "M1E":
		case "M1J":
		case "M1K":
		case "M1L":
		case "M1M":
		case "M1N":
		case "M2L":
		case "M2P":
		case "M3B":
		case "M3C":
		case "M4C":
		case "M4E":
		case "M4G":
		case "M4H":
		case "M4J":
		case "M4K":
		case "M4L":
		case "M4M":
		case "M4P":
		case "M4S":
		case "M4T":
		case "M4W":
		case "M4X":
		case "M4Y":
		case "M5A":
		case "M5B":
		case "M5C":
		case "M5E":
		case "M5G":
		case "M5H":
		case "M5J":
		case "M5K":
		case "M5L":
		case "M5S":
		case "M5T":
		case "M5V":
		case "M5W":
		case "M5X":
			 comp = 'KKTorontoEast';		
		break;		
		case "L0G":
		case "L0J":
		case "L3X":
		case "L3Y":
		case "L3Z":
		case "L4H":
		case "L4L":
		case "L4T":
		case "L4V":
		case "L6A":
		case "L7B":
		case "L9N":
		case "M9L":
		case "M9M":
		case "M9N":
		case "M9P":
		case "M9R":
		case "M9V":
		case "M9W":
			 comp = 'KKVaughan';		
		break;		
		case "L0B":
		case "L0C":
		case "L0E":
		case "L0H":
		case "L1B":
		case "L1C":
		case "L1E":
		case "L1G":
		case "L1H":
		case "L1J":
		case "L1K":
		case "L1L":
		case "L1M":
		case "L1N":
		case "L1P":
		case "L1R":
		case "L1S":
		case "L1T":
		case "L1V":
		case "L1W":
		case "L1X":
		case "L1Y":
		case "L1Z":
		case "L4A":
		case "L4G":
		case "L4P":
		case "L9L":
		case "L9P":
			 comp = 'KKYorkDurham';		
		break;		
		default:
			comp = 'KKFI - Tom';
		break;
	}
	
	     if (comp=='KKOttawa') 			document.forms[formname].companycode.value='6D9E2FA27DBEAE929B519A6B58B145EC'; 
	else if (comp=='KKBarrie') 			document.forms[formname].companycode.value='639C238F319290000CD37277B042D8FD'; 
	else if (comp=='KKBurOak') 			document.forms[formname].companycode.value='3CB3C6432A62883C2E09BCF04B6C2567'; 
	else if (comp=='KKCalgary South') 	document.forms[formname].companycode.value='A6932358BD18D42CA85A5984B6FD7B42'; 
	else if (comp=='KKCalgary West') 	document.forms[formname].companycode.value='FFB548EC59FB33737A92F0ED97FA093A'; 
	else if (comp=='KKEdmonton South') 	document.forms[formname].companycode.value='0C2313A173E8E4CBF2596563463D80D0'; 
	else if (comp=='KKFI - Julia') 		document.forms[formname].companycode.value='4B943C43F7D83EF1AFD7D3D3526809CE';  // before moving to TOM: 6376AAC1E6EA8E16428E7CD694B78561
	else if (comp=='KKFI - Shaun') 		document.forms[formname].companycode.value='4B943C43F7D83EF1AFD7D3D3526809CE';  // before moving to TOM: AA0EE107BC427C020B421E16E53B896A
	else if (comp=='KKFI - Tom') 		document.forms[formname].companycode.value='4B943C43F7D83EF1AFD7D3D3526809CE'; 
	else if (comp=='KKKawartha Lakes') 	document.forms[formname].companycode.value='2435E1662C976AEBDE2FA4ABD910125D'; 
	else if (comp=='KKMarkham') 		document.forms[formname].companycode.value='B2B898D77770562477AF82C962952230'; 
	else if (comp=='KKOttawa') 			document.forms[formname].companycode.value='6D9E2FA27DBEAE929B519A6B58B145EC'; 
	else if (comp=='KKTorontoEast') 	document.forms[formname].companycode.value='E3197768C2FD87A413377ACB03FDD615'; 
	else if (comp=='KKVaughan') 		document.forms[formname].companycode.value='3B8CE2D07B93E5D2FA5760BF6A8B9B03'; 
	else if (comp=='KKYorkDurham') 		document.forms[formname].companycode.value='B477AE7EC4F7A93C3ECFA169FB1C5E9D'; 
	else 								document.forms[formname].companycode.value='4B943C43F7D83EF1AFD7D3D3526809CE'; // KKFI - Tom
			
}





/*
------------------------------------------------------------------------------------------------------------------------------------------------
AJAX functions
------------------------------------------------------------------------------------------------------------------------------------------------	
*/

	/* FUNCTION: Ajax Request via POST.
		PARAMS:		strURL:			target URL
					strSubmit:		POST string, URL format ('&param=value' etc.);
					strResultFunc:	NAME (string) of function to be executed on reply	
	*/
	function ajaxReq(strURL, strSubmit, strResultFunc) {
		var xmlHttpReq = false;
		// Mozilla/Safari/IE7
		if (window.XMLHttpRequest) 
		{			
			xmlHttpReq = new XMLHttpRequest();
			if (xmlHttpReq.overrideMimeType)
				xmlHttpReq.overrideMimeType('text/xml'); // this would break IE7..
		}
		// <=IE6
		else if (window.ActiveXObject) 
		{		
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
		xmlHttpReq.open('POST', strURL, true);
		xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			if (xmlHttpReq.readyState == 4) {
				if (xmlHttpReq.status == 200) {
					if (strResultFunc) { // Run result function
						if (typeof strResultFunc == "string")
						eval(strResultFunc+'(xmlHttpReq);');
						else
							eval(strResultFunc(xmlHttpReq));
					}
					else // if no result function was specified, simply return true.
						return true;
				}
			}
        }
       	xmlHttpReq.send(strSubmit);
	}

		
	/*	FUNCTION:	XML document fixer for IE6-. IE doesn't read responseXML, this should fix that.
		 			Drop your xmlHTTPReq elements here and the function would return XML objects.
		
		PARAMS: 	Request:	XmlHTTPRequest object
	*/
	function xmlFix(request) 
	{
		// for standard compliant browsers
		if (document.implementation && document.implementation.createDocument) {
			return request.responseXML;
		}
		//Internet explorer
		else if (window.ActiveXObject){
		// Search and destroy (or at least rename) older nodes. 
		// This is To avoid multiple nodes (getElementById problem)
			var oldung = document.getElementById('_formjAjaxReturnXML');
			if (oldung) oldung.id = 'obsolete';
		// Now the real deal happens
			testandoAppend = document.createElement('xml');
			testandoAppend.setAttribute('innerHTML',request.responseText);
			testandoAppend.setAttribute('id','_formjAjaxReturnXML');
			document.body.appendChild(testandoAppend);
			xmlDoc = document.getElementById('_formjAjaxReturnXML');
			return xmlDoc;
		}
		else {
		//If the browser doesn't support xml
			alert('Your browser can\'t handle XML documents, please contact support.');
		}
	}
		
	
	//////////////////////////////////// xml2array() ////////////////////////////////////////
	//See http://www.openjs.com/scripts/xml_parser/
	var not_whitespace = new RegExp(/[^\s]/);//This can be given inside the funciton - I made it a global variable to make the script a little bit faster.
	var parent_count;
	//Process the xml data
	function xml2array(xmlDoc,parent_count) 
	{
		var arr;
		var parent = "";
		parent_count = parent_count || new Object;
	
		var attribute_inside = 0; /*:CONFIG: Value - 1 or 0
		*	If 1, Value and Attribute will be shown inside the tag - like this...
		*	For the XML string...
		*	<guid isPermaLink="true">http://www.bin-co.com/</guid>
		*	The resulting array will be...
		*	array['guid']['value'] = "http://www.bin-co.com/";
		*	array['guid']['attribute_isPermaLink'] = "true";
		*	
		*	If 0, the value will be inside the tag but the attribute will be outside - like this...	
		*	For the same XML String the resulting array will be...
		*	array['guid'] = "http://www.bin-co.com/";
		*	array['attribute_guid_isPermaLink'] = "true";
		*/
		
		if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") 
		{			
			if(xmlDoc.childNodes.length > 1) { //If its a parent
				arr = new Object;
				parent = xmlDoc.nodeName;
				
			}
		}
		var value = xmlDoc.nodeValue;
		if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value) 
		{
			if(not_whitespace.test(value)) {//If its a child
				arr = new Object;
				arr[xmlDoc.parentNode.nodeName] = value;
			}
		}
	
	

		if(xmlDoc.childNodes.length) {
			if(xmlDoc.childNodes.length == 1) { //Just one item in this tag.
				arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:
			} else { //If there is more than one childNodes, go thru them one by one and get their results.
				var index = 0;
	
				for(var i=0; i<xmlDoc.childNodes.length; i++) {//Go thru all the child nodes.
					var temp = xml2array(xmlDoc.childNodes[i],parent_count); //:RECURSION:
					if(temp) {
						var assoc = false;
						var arr_count = 0;
						for(key in temp) {
							if(isNaN(key)) assoc = true;
							arr_count++;
							if(arr_count>2) break;//We just need to know wether it is a single value array or not
						}
						
						if(assoc && arr_count == 1) {
							if (typeof arr === "undefined")
								continue;
							
							if(arr[key]) { 	//If another element exists with the same tag name before,						
											//		put it in a numeric array.
								//Find out how many time this parent made its appearance
								if(!parent_count || !parent_count[key]) {
									parent_count[key] = 0;
	
									var temp_arr = arr[key];
									arr[key] = new Object;
									arr[key][0] = temp_arr;
								}
								parent_count[key]++;
								arr[key][parent_count[key]] = temp[key]; //Members of of a numeric array
							} else {							
								parent_count[key] = 0;
								arr[key] = temp[key];
								if(xmlDoc.childNodes[i].attributes)
								if(xmlDoc.childNodes[i].attributes.length) {
									for(var j=0; j<xmlDoc.childNodes[i].attributes.length; j++) {
										var nname = xmlDoc.childNodes[i].attributes[j].nodeName;
										if(nname) {
											/* Value and Attribute inside the tag */
											if(attribute_inside) {
												var temp_arr = arr[key];
												arr[key] = new Object;
												arr[key]['value'] = temp_arr;
												arr[key]['attribute_'+nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
											} else {
											/* Value in the tag and Attribute otside the tag(in parent) */
												arr['attribute_' + key + '_' + nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
											}
										}
									} //End of 'for(var j=0; j<xmlDoc. ...'  
								} //End of 'if(xmlDoc.childNodes[i] ...'  
							}
						} else {
							arr[index] = temp;
							index++;
						}
					} //End of 'if(temp) {'
				} //End of 'for(var i=0; i<xmlDoc. ...'
			}
		}
	
		if(parent && arr) {
			var temp = arr;
			arr = new Object;
			
			arr[parent] = temp;
		}
		return arr;
	}	
		
		
	function dump(arr,level) 
	{
		var dumped_text = "";
		if(!level) level = 0;
		
		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";
		
		if(typeof(arr) == 'object') { //Array/Hashes/Objects
		 for(var item in arr) {
		  var value = arr[item];
		 
		  if(typeof(value) == 'object') { //If it is an array,
		   dumped_text += level_padding + "'" + item + "' ...\n";
		   dumped_text += dump(value,level+1);
		  } else {
		   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
		  }
		 }
		} else { //Stings/Chars/Numbers etc.
		 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		} 
		return dumped_text;  
	} 	
		



































	
	
	