﻿var _ContactFormURL = 'http://www.provantageweb.com/ContactForm/ContactUs.aspx';
var _defaultTopDomain = 'com';

function BuildContactFormURL(defaultSubDomain) {
  var pc = GetUrlParam(document.URL, 'pc');
  if( !pc ) {
    var ifURL = _ContactFormURL;
    var ParentURL = document.URL;
    var REQ = GetUrlParam(ParentURL, 'Request');
    var QUOTEID = GetUrlParam(ParentURL, 'QuoteRequestID');
    var SUGGEST = GetUrlParam(ParentURL, 'Suggest');
    var DIMEN = GetUrlParam(ParentURL, 'Dimensions');
    
    // Determine TD (top domain) from the actual parent URL.
    var TD = GetUrlParam(ParentURL, 'td');
    if( !TD ) {
      TD = GetRegExChoice(ParentURL, '.(com|ca)[\:\/]');
    }

    if( !TD ) {
      // Default to US/.com
      TD = _defaultTopDomain;
    }

    // Determine SD (sub domain) from current URL.
    var SD = GetUrlParam(ParentURL, 'sd' );
    if( !SD ) {
      SD = defaultSubDomain;
    }
    
    // If the URL in the /requestQuote/ path, enable QUOTEID
    if( !QUOTEID ) {
      var patt = new RegExp('\/requestQuote\/index.php');
      if( patt.exec(ParentURL) ) {
        QUOTEID = '0';
      }
    }

    ifURL += '?td=' + TD + '&sd=' + SD
             + (REQ ? '&Request=' + REQ : '')
             + (QUOTEID ? '&QuoteRequestID=' + QUOTEID : '')
             + (SUGGEST ? '&Suggest=' + SUGGEST : '')
             + (DIMEN ? '&Dimensions=' + DIMEN : '')

    return ifURL;
  }
}

function GetRegExChoice(s,choiceRegEx) {
  var patt = new RegExp(choiceRegEx);
  var found = patt.exec(s);
  var value = null;
  if( found ) {
    value = new String(found).split(',')[1];
  }
  return value;
}

function GetUrlParam(url,urlParam) {
  var patt = new RegExp(urlParam + '=([^&]*)','i');
  var found = patt.exec(url);
  var value = null;
  if( found ) {
    var s = new String(found);
    value = s.split(',')[1];
  }
  return value;
}

